Skip to content

Commit

Permalink
API Remove outdated API that allows searching for pages by content
Browse files Browse the repository at this point in the history
This relied on the weird non-standard way CMSMain page filtering worked.
This same functionality could be reintroduced by replacing the
SiteTreeSearchContext with a custom one that filters by callback, but it
won't be available in this module.
  • Loading branch information
GuySartorelli committed Feb 27, 2025
1 parent a97aead commit 89858aa
Show file tree
Hide file tree
Showing 10 changed files with 0 additions and 514 deletions.
8 changes: 0 additions & 8 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ SilverStripe\CMS\Controllers\ContentController:
url_handlers:
'element/$ID!': 'handleElement'

SilverStripe\CMS\Controllers\CMSMain:
extensions:
- DNADesign\Elemental\Extensions\ElementalCMSMainExtension

SilverStripe\CMS\Model\SiteTree:
extensions:
topPageSiteTreeExtension: DNADesign\Elemental\Extensions\TopPageSiteTreeExtension
Expand All @@ -35,7 +31,3 @@ SilverStripe\Versioned\VersionedGridFieldItemRequest:
Symbiote\GridFieldExtensions\GridFieldAddNewMultiClassHandler:
extensions:
- DNADesign\Elemental\Extensions\GridFieldAddNewMultiClassHandlerExtension

SilverStripe\Core\Injector\Injector:
SilverStripe\CMS\Controllers\CMSSiteTreeFilter_Search:
class: DNADesign\Elemental\Controllers\ElementSiteTreeFilterSearch
30 changes: 0 additions & 30 deletions docs/en/03_searching-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,3 @@ to make it clear in search results where one piece of content ends and another b
Page:
search_index_element_delimiter: ' ... '
```

## CMS page search

CMS page search will include search results for pages with elements that match the search query.

By default it uses the same method as the search indexing where it will fully render every element that is
being searched. This is an expensive operation and can cause performance issues if you have a large site with a lot of elements.

To increase performance by a large amount, likely more than doubling it, you can disable the rendering of elements and instead just look at the database values of the elements directly.

```yml
DNADesign\Elemental\Controllers\ElementSiteTreeFilterSearch:
render_elements: false
```

If `render_elements` is `false`, then all fields that have stored as a Varchar or Text like are searched. Individual fields on blocks can be excluded from the search by adding fields to the `exclude_fields_from_cms_search` array config variable on the element class. e.g.

```yml
App\MyElement:
fields_excluded_from_cms_search:
- MyFieldToExclude
- AnotherFieldToExclude
```

If the above is still not performant enough, searching elements for content in CMS page search can be disabled entirely:

```yml
DNADesign\Elemental\Controllers\ElementSiteTreeFilterSearch:
search_for_term_in_content: false
```
146 changes: 0 additions & 146 deletions src/Controllers/ElementSiteTreeFilterSearch.php

This file was deleted.

37 changes: 0 additions & 37 deletions src/Extensions/ElementalCMSMainExtension.php

This file was deleted.

25 changes: 0 additions & 25 deletions src/Extensions/ElementalPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use DNADesign\Elemental\Models\ElementalArea;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Dev\Deprecation;
use SilverStripe\View\Parsers\HTMLValue;
use SilverStripe\View\SSViewer;

Expand Down Expand Up @@ -85,30 +84,6 @@ public function getElementsForSearch()
return implode($this->owner->config()->get('search_index_element_delimiter') ?? '', $output);
}

/**
* Returns the contents of all Elements on the pages ElementalAreas for use in CMS search
* @deprecated 5.4.0 Will be removed without equivalent functionality
*/
public function getContentFromElementsForCmsSearch(): string
{
Deprecation::noticeWithNoReplacment('5.4.0');
$output = [];
$elements = $this->getEagerLoadedElements();
/** @var BaseElement $element */
foreach ($elements as $element) {
if (!$element->getSearchIndexable()) {
continue;
}
$content = $element->getContentForCmsSearch();
if ($content) {
$output[] = $content;
}
}
// Use |%| to delimite different elements rather than space so that you don't
// accidentally join results of two elements that are next to each other in a table
return implode('|%|', $output);
}

/**
* @see SiteTree::getAnchorsOnPage()
*/
Expand Down
64 changes: 0 additions & 64 deletions src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,6 @@ class BaseElement extends DataObject implements CMSPreviewable
*/
private static $class_description = 'Base element class';

/**
* List of fields to exclude from CMS SiteTree seatch
* @see ElementSiteTreeFilterSearch::applyDefaultFilters()
* @deprecated 5.4.0 Will be removed without equivalent functionality
*/
private static array $fields_excluded_from_cms_search = [
'ExtraClass',
];

private static $db = [
'Title' => 'Varchar(255)',
'ShowTitle' => 'Boolean',
Expand Down Expand Up @@ -532,61 +523,6 @@ public function getContentForSearchIndex(): string
return $content;
}

/**
* Provides content for CMS search if ElementSiteTreeFilterSearch.render_elements is false
* @deprecated 5.4.0 Will be removed without equivalent functionality
*/
public function getContentForCmsSearch(): string
{
Deprecation::noticeWithNoReplacment('5.4.0');
$fieldNames = $this->getTextualDatabaseFieldNames();
$excludedFieldNames = $this->getFieldNamesExcludedFromCmsSearch();
$contents = [];
foreach ($fieldNames as $fieldName) {
if (in_array($fieldName, $excludedFieldNames)) {
continue;
}
$contents[] = $this->$fieldName;
}
// Allow projects to update contents of third-party elements.
$this->extend('updateContentForCmsSearch', $contents);

// Use |#| to delimit different fields rather than space so that you don't
// accidentally join results of two columns that are next to each other in a table
$content = implode('|#|', array_filter($contents));

// Strips tags and be sure there's a space between words.
$content = trim(strip_tags(str_replace('<', ' <', $content)));

return $content;
}

/**
* Get field names that have a Varchar or Text like type in the database
*/
private function getTextualDatabaseFieldNames(): array
{
$fieldNames = [];
$textualDatabaseFields = DataObject::getSchema()->databaseFields($this);
foreach ($textualDatabaseFields as $fieldName => $databaseFieldType) {
$lcType = strtolower(strtok($databaseFieldType ?? '', '('));
if (str_contains($lcType, 'varchar') || str_contains($lcType, 'text')) {
$fieldNames[] = $fieldName;
}
}
return $fieldNames;
}

private function getFieldNamesExcludedFromCmsSearch(): array
{
return [
// `fixed_fields` contains ['ID', 'ClassName', 'LastEdited', 'Created'] and possibly more
...array_keys(static::config()->get('fixed_fields')),
// manually excluded fields
...static::config()->get('fields_excluded_from_cms_search')
];
}

/**
* Default way to render element in templates. Note that all blocks should
* be rendered through their {@link ElementController} class as this
Expand Down
9 changes: 0 additions & 9 deletions tests/BaseElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,15 +493,6 @@ public function testPreviewLink(string $class, string $elementIdentifier, ?strin
}
}

public function testGetContentForCmsSearch()
{
$element = $this->objFromFixture(ElementContent::class, 'content1');
$this->assertSame('Test Content', $element->getContentForCmsSearch());
$element = $this->objFromFixture(TestElement::class, 'elementDataObject3');
$this->assertSame('Hello Test|#|Element 3', $element->getContentForCmsSearch());
}


public function testGetPage()
{
$element = $this->objFromFixture(ElementContent::class, 'content1');
Expand Down
Loading

0 comments on commit 89858aa

Please sign in to comment.