Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Search Terms for Searchable Pages #42

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/SearchTerm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Werkbot\Search;

use SilverStripe\ORM\DataObject;

class SearchTerm extends DataObject
{
private static $singular_name = 'Search Term';
private static $plural_name = 'Search Terms';
private static $table_name = 'SearchTerm';

private static $db = [
'SearchTermText' => 'Text',
'SortOrder' => 'Int',
];

private static $has_one = [
'SearchTermOf' => DataObject::class,
];
}
72 changes: 72 additions & 0 deletions src/SearchableExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This being under settings makes sense for pages, but for other searchable dataobjects, like articles and content layouts, they do not have settings fields.

Copy link
Member Author

@aletail aletail May 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I did not consider Blog articles...

  • I was trying to avoid adding to the CMS fields, simply to not clutter it up any more
  • Layouts I am fine without them noy having the option, as they are not searched directly as in they reference a page
  • Articles though...they should have the setting somewhere

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tiller1010 Take a look at this again, I updated so the search terms are added to all objects

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to mention the content layout, blog, etc....need their getIndexQuery functions updated to include search terms. I figure I would do that after this is merged and released

{
$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
Expand All @@ -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'";
**/
Expand Down
Loading