Site Search on Sitecore-4 (4.5) - How to enable it?

Apologies for not being clear in my question above, I tried to amend it but it’s against the rules, I also noticed I wrote Sitecore (which is de CMS I use on a daily basis on my work) as opposed to Silverstripe, which is the one I am trying to learn on my free time as a personal quarantine project.

Please, if you are a moderator, can you rename ‘Sitecore’ with ‘Silverstripe’ in the title? Thanks!

Clarifying and making it clearer my struggle was to enable the search form on Silverstripe 4.5.

I found plenty of documentation about it, however, the info needed is not in one place, therefore if you are not a experienced Silverstripe developer, it’s not that clear what you have to do on v4, so I hope my mini steps below can help other people as I spent hours myself :face_with_head_bandage: trying to figure this out, and by the amount of unanswered questions I found, it’s probably other people’s struggle too.

  • I am using a fresh installed version of Silverstripe 4.5 and the simple theme (composer create-project silverstripe/installer my-project).
  • No other Themes or add-ons installed

I used this Sitecore 3 tutorial (https://docs.silverstripe.org/en/3/tutorials/site_search) as a base for my research:

Step 1 on v3: Add the code below to mysite/_config.php

FulltextSearchable::enable();

Changes to v4?: After reading other similar queries in this forum (Site Search) and also in stackoverflow (full text search - Enable FullTextSearch in Silverstripe 4 - Stack Overflow), people’s answers pointed that on SS v4, one should add the code below instead:

SilverStripe\ORM\Search\FulltextSearchable::enable();

This is how my app/_config.php file looks after adding the suggested line:

<?php

use SilverStripe\Security\PasswordValidator;

use SilverStripe\Security\Member;

// remove PasswordValidator for SilverStripe 5.0

$validator = PasswordValidator::create();

// Settings are registered via Injector configuration - see passwords.yml in framework

Member::set_password_validator($validator);

SilverStripe\ORM\Search\FulltextSearchable::enable();

Step 2 on v3: add $SearchForm anywhere in the templates.

Changes to v4?: In SS4 the simple theme already contains this code in themes/simple/templates/Includes/Header.ss
This is how my Header.ss looks without any changes from a fresh install:

<header class="header" role="banner">
	<div class="inner">
		<div class="unit size4of4 lastUnit">
			<a href="$BaseHref" class="brand" rel="home">
				<h1>$SiteConfig.Title</h1>
				<% if $SiteConfig.Tagline %>
				<p>$SiteConfig.Tagline</p>
				<% end_if %>
			</a>
			<% if $SearchForm %>
				<span class="search-dropdown-icon">L</span>
				<div class="search-bar">
					$SearchForm
				</div>
			<% end_if %>
			<% include Navigation %>
		</div>
	</div>
</header>

According to the v3 Tutorial, that’s it, run dev/build?flush=all and the search field appears. However doing more research, for SS V4 there are extra steps needed.

Step 3 on V4: According to this answer (SS 4.0 Site Search - How to enable? - #3 by John_Broadwater) and this one (Silverstripe 4 - How do you enable FullTextSearchable and $SearchForm? - Stack Overflow), on SS v4 the extra step is to add the following code to the file src/Page.php:

    use SilverStripe\ORM\DataObject;
    use SilverStripe\ORM\Connect\MySQLSchemaManager;

    class MyDataObject extends DataObject
    {
        private static $create_table_options = [
            MySQLSchemaManager::ID => 'ENGINE=MyISAM'
        ];
    }

This is how my src/Page.php file looks after adding the suggested code:

<?php

namespace {

    use SilverStripe\CMS\Model\SiteTree;

    class Page extends SiteTree
    {
        private static $db = [];

        private static $has_one = [];
    }

    use SilverStripe\ORM\DataObject;
    use SilverStripe\ORM\Connect\MySQLSchemaManager;

    class MyDataObject extends DataObject
    {
        private static $create_table_options = [
            MySQLSchemaManager::ID => 'ENGINE=MyISAM'
        ];
    }
}

Now if you run dev/build?flush=all the search field should appear:

Other answers I found online suggested that changes were needed to the files below, however I didn’t need to make any changes to any other file to make the search field appear. It might be the case for other themes or modules, but not for a fresh installation with the simple theme:

  • app/_config/theme.yml
  • app/src/PageController.php

:smiley: