PHP code example of arillo / silverstripe-simple-search

1. Go to this page and download the library: Download arillo/silverstripe-simple-search library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

arillo / silverstripe-simple-search example snippets



use SilverStripe\CMS\Model\SiteTree;
use Arillo\SimpleSearch\ISearchIndexable;
use Arillo\SimpleSearch\SearchableExtension;
use Arillo\SimpleSearch\SearchIndexEntry;

class Page extends SiteTree implements ISearchIndexable // you need to implement this interface
{
    private static $extensions = [
        // adds after write/delete hooks to re-index this record
        SearchableExtension::class,
    ];

    // add interface function, it should return the string you want to add to your search index.
    public function forSearchIndex()
    {
        $contents = [
            $this->Title,
            $this->obj('Content')
                ->setProcessShortcodes(true)
                ->RAW()
        ];

        $string = implode($contents, ' ');
        return $string ? SearchIndexEntry::sanitize_string($string) : null;
    }
}



use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\View\SSViewer;
use SilverStripe\Core\Config\Config;
use Arillo\SimpleSearch\ISearchIndexable;
use Arillo\SimpleSearch\SearchableExtension;
use Arillo\SimpleSearch\SearchIndexEntry;
use Arillo\Elements\ElementsExtension;

class Page extends SiteTree implements ISearchIndexable // you need to implement this interface
{
	const SECTIONS = 'Sections';

    private static $extensions = [
        // adds after write/delete hooks to re-index this record
        SearchableExtension::class,
    ];

    // add interface function, it should return the string you want to add to your search index.
    public function forSearchIndex()
    {
	 if ($this->isPageWithSections()) {
            $oldThemes = SSViewer::get_themes();
            SSViewer::set_themes(
                Config::inst()->get(SSViewer::class, 'themes')
            );
            try {
                $string = SearchIndexEntry::sanitize_string(
                    $this->customise([
                        'RelationName' => self::SECTIONS
                    ])->renderWith('Elements')
                );
            } finally {
                SSViewer::set_themes($oldThemes);
            }
            return $string;
        }

        $contents = [
            $this->Title,
            $this->obj('Content')
                ->setProcessShortcodes(true)
                ->RAW()
        ];

        $string = implode($contents, ' ');
        return $string ? SearchIndexEntry::sanitize_string($string) : null;
    }
    
    public function isPageWithSections()
    {
        return isset(
            ElementsExtension::page_element_relation_names($this)[
                self::SECTIONS
            ]
        );
    }
}

php vendor/silverstripe/framework/cli-script.php dev/tasks/Arillo-SimpleSearch-BuildIndexTask