Skip to content

Commit

Permalink
Include 'ShowInSearch' field in search extension
Browse files Browse the repository at this point in the history
This ensures that the field exists for all classes that are configured to be included in the search index
  • Loading branch information
scott-nz committed Mar 25, 2024
1 parent 79b5e78 commit fdc57e1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
6 changes: 3 additions & 3 deletions src/Extensions/FileFormFactoryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -28,7 +29,6 @@ public function updateFormFields(FieldList $fields, $controller, $formName, $con

$fields->addFieldToTab('Editor.Details', $indexedField);
$fields->addFieldToTab('Editor.Details', $showInSearchField);

}
}

}
39 changes: 36 additions & 3 deletions src/Extensions/SearchServiceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 <strong>all</strong> 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);
}
}

Expand Down

0 comments on commit fdc57e1

Please sign in to comment.