From d7cd9e39072f2f83c547be5d02168275f830f12a Mon Sep 17 00:00:00 2001 From: Scott Sutherland Date: Mon, 25 Mar 2024 13:46:36 +1300 Subject: [PATCH] Include 'ShowInSearch' field in search extension This ensures that the field exists for all classes that are configured to be included in the search index --- .gitignore | 1 + src/Extensions/FileFormFactoryExtension.php | 5 +-- src/Extensions/SearchServiceExtension.php | 39 +++++++++++++++++++-- 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/src/Extensions/FileFormFactoryExtension.php b/src/Extensions/FileFormFactoryExtension.php index 37c04f9..4b56ada 100644 --- a/src/Extensions/FileFormFactoryExtension.php +++ b/src/Extensions/FileFormFactoryExtension.php @@ -13,7 +13,8 @@ */ class FileFormFactoryExtension extends Extension { - public function updateFormFields(FieldList $fields, $controller, $formName, $context): void + + public function updateFormFields(FieldList $fields): void { $indexedField = DatetimeField::create( @@ -30,5 +31,5 @@ public function updateFormFields(FieldList $fields, $controller, $formName, $con $fields->addFieldToTab('Editor.Details', $showInSearchField); } -} +} diff --git a/src/Extensions/SearchServiceExtension.php b/src/Extensions/SearchServiceExtension.php index 63f8cb0..aa5ac5e 100644 --- a/src/Extensions/SearchServiceExtension.php +++ b/src/Extensions/SearchServiceExtension.php @@ -5,6 +5,7 @@ use Exception; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Injector\Injectable; +use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\ReadonlyField; use SilverStripe\ORM\DataExtension; @@ -37,6 +38,11 @@ class SearchServiceExtension extends DataExtension private static array $db = [ 'SearchIndexed' => 'Datetime', + 'ShowInSearch' => 'Boolean(1)', + ]; + + private static array $defaults = [ + 'ShowInSearch' => '1', ]; private bool $hasConfigured = false; @@ -59,12 +65,39 @@ public function updateCMSFields(FieldList $fields): void return; } - $field = ReadonlyField::create('SearchIndexed', _t(self::class.'.LastIndexed', 'Last indexed in search')); + $indexedField = ReadonlyField::create( + 'SearchIndexed', + _t(self::class . '.LastIndexed', 'Last indexed in search') + ); + + $showInSearchField = CheckboxField::create( + 'ShowInSearch', + _t(self::class . '.ShowInSearch', 'Show in search') + ); + + if (!$this->getOwner()->hasExtension(Versioned::class)) { + $showInSearchField->setDescription( + _t( + self::class . 'ShowInSearch_Description', + 'This setting will apply to all published and unpublished versions of this record.' + ) + ); + } if ($fields->hasTabSet()) { - $fields->addFieldToTab('Root.Main', $field); + $fields->addFieldToTab('Root.Main', $indexedField); + + // In the case of SiteTree objects, we want this checkbox in the settings tab alongside other access + // restriction settings. If there isn't already a Settings tab, it would be poor UI to add it for one + // specific field. + if ($fields->findTab('Root.Settings')) { + $fields->addFieldToTab('Root.Settings', $showInSearchField); + } else { + $fields->addFieldToTab('Root.Main', $showInSearchField); + } } else { - $fields->push($field); + $fields->push($indexedField); + $fields->push($showInSearchField); } }