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

Show in search check for files #92

Merged
merged 13 commits into from
May 2, 2024
3 changes: 3 additions & 0 deletions _config/extensions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ SilverStripe\ORM\FieldType\DBHTMLVarchar:
Symbiote\QueuedJobs\Controllers\QueuedJobsAdmin:
extensions:
- SilverStripe\SearchService\Extensions\QueuedJobsAdminExtension
SilverStripe\AssetAdmin\Forms\FileFormFactory:
extensions:
- SilverStripe\SearchService\Extensions\SearchFormFactoryExtension
---
Name: 'silverstripe-search-service-cms'
Only:
Expand Down
51 changes: 51 additions & 0 deletions src/Extensions/SearchFormFactoryExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace SilverStripe\SearchService\Extensions;

use SilverStripe\Assets\Image;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DatetimeField;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormFactory;

class SearchFormFactoryExtension extends Extension
{

public function updateForm(
Form $form,
?RequestHandler $controller = null,
string $name = FormFactory::DEFAULT_NAME,
array $context = []
): void {
$fields = $form->Fields()->findOrMakeTab('Editor.Details');
$file = $context['Record'] ?? null;

if (!$fields || !$file || $file instanceof Image) {
return;
}

$fields->push(
CheckboxField::create(
'ShowInSearch',
_t(
'SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.SHOWINSEARRCH',
'Show in search?'
)
)
);

$fields->push(
DatetimeField::create(
'SearchIndexed',
_t(
'SilverStripe\\SearchService\\Extensions\\SearchServiceExtension.LastIndexed',
'Last indexed in search'
)
)
->setReadonly(true)
);
}

}
15 changes: 12 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 @@ -36,9 +37,14 @@ class SearchServiceExtension extends DataExtension
use BatchProcessorAware;

private static array $db = [
'ShowInSearch' => 'Boolean',
'SearchIndexed' => 'Datetime',
];

private static $defaults = [
'ShowInSearch' => true,
];

private bool $hasConfigured = false;

public function __construct(
Expand All @@ -59,12 +65,15 @@ public function updateCMSFields(FieldList $fields): void
return;
}

$field = ReadonlyField::create('SearchIndexed', _t(self::class.'.LastIndexed', 'Last indexed in search'));
$showInSearchField = CheckboxField::create("ShowInSearch", _t(self::class . '.ShowInSearch', 'Show in search?'));
$searchIndexedField = ReadonlyField::create('SearchIndexed', _t(self::class . '.LastIndexed', 'Last indexed in search'));

if ($fields->hasTabSet()) {
$fields->addFieldToTab('Root.Main', $field);
$fields->addFieldToTab('Root.Main', $showInSearchField);
$fields->addFieldToTab('Root.Main', $searchIndexedField);
} else {
$fields->push($field);
$fields->push($showInSearchField);
$fields->push($searchIndexedField);
}
}

Expand Down
13 changes: 13 additions & 0 deletions tests/DataObject/DataObjectDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,4 +758,17 @@ public function testIndexDataObjectDocument(): void
});
}

public function testIndexDataObjectDocumentShowInSearch(): void
{
// Document should be NOT indexable (as its value is changed to ShowInSearch: 0)
$dataObject = $this->objFromFixture(DataObjectFakeVersioned::class, 'one');
$dataObject->ShowInSearch = false;
$dataObject->write();
$this->assertFalse($dataObject->shouldIndex());

// Document should be indexable (as its value is now changed to ShowInSearch: 1)
$dataObject->ShowInSearch = true;
$dataObject->write();
$this->assertTrue($dataObject->shouldIndex());
}
}
1 change: 0 additions & 1 deletion tests/Fake/DataObjectFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class DataObjectFake extends DataObject implements TestOnly

private static array $db = [
'Title' => 'Varchar',
'ShowInSearch' => 'Boolean',
'Sort' => 'Int',
];

Expand Down
31 changes: 29 additions & 2 deletions tests/Fake/DataObjectFakeVersioned.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
use SilverStripe\SearchService\Extensions\SearchServiceExtension;
use SilverStripe\SearchService\Interfaces\DocumentInterface;
use SilverStripe\Versioned\Versioned;

/**
Expand All @@ -13,7 +14,7 @@
* @mixin SearchServiceExtension
* @mixin Versioned
*/
class DataObjectFakeVersioned extends DataObject implements TestOnly
class DataObjectFakeVersioned extends DataObject implements TestOnly, DocumentInterface
{

private static string $table_name = 'DataObjectFakeVersioned';
Expand All @@ -25,12 +26,38 @@ class DataObjectFakeVersioned extends DataObject implements TestOnly

private static array $db = [
'Title' => 'Varchar',
'ShowInSearch' => 'Boolean',
];

public function canView(mixed $member = null): bool
{
return true;
}

public function getIdentifier(): string
{
return (string) $this->ID;
}

public function shouldIndex(): bool
{
return (bool) $this->ShowInSearch;
}

public function markIndexed(): void
{
$this->ShowInSearch = 1;
}

public function toArray(): array
{
return [
'Title' => $this->Title,
];
}

public function getSourceClass(): string
{
return static::class;
}

}