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..eeb95b8 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( @@ -28,7 +29,6 @@ public function updateFormFields(FieldList $fields, $controller, $formName, $con $fields->addFieldToTab('Editor.Details', $indexedField); $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); } }