From 7a86d48be286f6a1297f9480a0573d4477878eb4 Mon Sep 17 00:00:00 2001 From: Jay Richardson Date: Wed, 29 May 2024 13:09:33 -0400 Subject: [PATCH 1/2] Added Search Terms for Searchable Pages --- src/SearchTerm.php | 21 +++++++++++ src/SearchableExtension.php | 72 +++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/SearchTerm.php diff --git a/src/SearchTerm.php b/src/SearchTerm.php new file mode 100644 index 0000000..1d59f5f --- /dev/null +++ b/src/SearchTerm.php @@ -0,0 +1,21 @@ + 'Text', + 'SortOrder' => 'Int', + ]; + + private static $has_one = [ + 'SearchTermOf' => DataObject::class, + ]; +} diff --git a/src/SearchableExtension.php b/src/SearchableExtension.php index e82ea7a..d37fb21 100644 --- a/src/SearchableExtension.php +++ b/src/SearchableExtension.php @@ -2,12 +2,34 @@ namespace Werkbot\Search; +use SilverStripe\Forms\Tab; +use SilverStripe\Forms\TabSet; use SilverStripe\Core\ClassInfo; +use SilverStripe\Forms\FieldList; +use SilverStripe\Forms\TextField; +use SilverStripe\Forms\FieldGroup; use SilverStripe\ORM\DataExtension; +use SilverStripe\Forms\CheckboxField; use SilverStripe\Versioned\Versioned; +use SilverStripe\Forms\GridField\GridField; +use SilverStripe\Forms\GridField\GridFieldConfig; +use SilverStripe\Forms\GridField\GridFieldButtonRow; +use SilverStripe\Forms\GridField\GridFieldDeleteAction; +use SilverStripe\Forms\GridField\GridFieldToolbarHeader; +use Symbiote\GridFieldExtensions\GridFieldOrderableRows; +use Symbiote\GridFieldExtensions\GridFieldEditableColumns; +use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton; class SearchableExtension extends DataExtension { + private static $has_many = [ + 'SearchTerms' => SearchTerm::class . '.SearchTermOf', + ]; + + private static $owns = [ + 'SearchTerms', + ]; + /* Column names for the "Title" and "Content" search fields Override these to set them to a different column name @@ -19,6 +41,52 @@ class SearchableExtension extends DataExtension "getSearchableTitle" => "Text", "getSearchableSummary" => 'HTMLText', ]; + + /** + * updateSettingsFields + * Adds the SearchTerms GridField to the settings tab + * This should only be applied to SiteTree objects + * + * @param FieldList $fields + * @return void + **/ + public function updateSettingsFields(FieldList $fields) + { + $fields->addFieldToTab('Root', new TabSet('Search', new Tab('Main'))); + + if ($this->owner->hasField("ShowInSearch")) { + $fields->removeByName('ShowInSearch'); + $ShowInSearch = CheckboxField::create("ShowInSearch", $this->owner->fieldLabel('ShowInSearch')); + $ShowInSearchGroup = FieldGroup::create( + 'Settings', + $ShowInSearch + ); + $fields->addFieldToTab('Root.Search.Main', $ShowInSearchGroup); + } + + $SearchTermsGridField = GridField::create( + 'SearchTerms', + 'Enter Search Terms', + $this->owner->SearchTerms(), + GridFieldConfig::create() + ->addComponent(GridFieldButtonRow::create('before')) + ->addComponent(GridFieldToolbarHeader::create()) + ->addComponent(GridFieldEditableColumns::create()) + ->addComponent(GridFieldDeleteAction::create()) + ->addComponent(GridFieldAddNewInlineButton::create()) + ->addComponent(new GridFieldOrderableRows('SortOrder')) + ); + + $SearchTermsGridField->getConfig()->getComponentByType(GridFieldEditableColumns::class)->setDisplayFields(array( + 'SearchTermText' => function ($record, $column, $grid) { + return TextField::create($column) + ->setAttribute('placeholder', 'Enter search term'); + } + )); + + $fields->addFieldToTab('Root.Search.Main', $SearchTermsGridField); + } + /** * getIndexQuery * This query is used when building the index @@ -36,6 +104,10 @@ class SearchableExtension extends DataExtension SiteTree ON SiteTree.ID = Page.ID + LEFT JOIN + SearchTerm + ON + SearchTerm.SearchTermOfID = Page.ID AND SearchTerm.SearchTermOfClass = SiteTree.ClassName WHERE SiteTree.ShowInSearch = '1'"; **/ From 6cb4926d56fd46a54e4e7cffe57465985502730c Mon Sep 17 00:00:00 2001 From: Jay Richardson Date: Thu, 30 May 2024 12:14:15 -0400 Subject: [PATCH 2/2] Updated so all objects receive the search settings SiteTree objects will have it added to their Settings tab. DataObjects will have it added to their main tab --- src/SearchableExtension.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/SearchableExtension.php b/src/SearchableExtension.php index d37fb21..0385350 100644 --- a/src/SearchableExtension.php +++ b/src/SearchableExtension.php @@ -5,6 +5,7 @@ use SilverStripe\Forms\Tab; use SilverStripe\Forms\TabSet; use SilverStripe\Core\ClassInfo; +use SilverStripe\ORM\DataObject; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\TextField; use SilverStripe\Forms\FieldGroup; @@ -41,7 +42,21 @@ class SearchableExtension extends DataExtension "getSearchableTitle" => "Text", "getSearchableSummary" => 'HTMLText', ]; - + /** + * updateCMSFields + * Adds the SearchTerms GridField to the CMS tab + * This should only be applied to Data objects + * + * @param FieldList $fields + * @return void + **/ + public function updateCMSFields(FieldList $fields) + { + if (DataObject::getSchema()->baseDataClass($this->owner->ClassName) != "SilverStripe\CMS\Model\SiteTree") { + $this->addSearchSettingFields($fields); + } + parent::updateCMSFields($fields); + } /** * updateSettingsFields * Adds the SearchTerms GridField to the settings tab @@ -51,6 +66,13 @@ class SearchableExtension extends DataExtension * @return void **/ public function updateSettingsFields(FieldList $fields) + { + if (DataObject::getSchema()->baseDataClass($this->owner->ClassName) == "SilverStripe\CMS\Model\SiteTree") { + $this->addSearchSettingFields($fields); + } + } + + public function addSearchSettingFields(FieldList &$fields) { $fields->addFieldToTab('Root', new TabSet('Search', new Tab('Main')));