Import a Hosted Search Experience from SearchStax Site Search into your Drupal site. You'll create a custom Drupal module to do this.
Prerequisites
Before you start, you'll need:
- A Drupal site (version 8 or higher)
- Access to your Drupal file system
- A SearchStax Site Search account with a configured search app
- Basic understanding of Drupal module development
- Drush command-line tool installed (used to clear Drupal cache)
Step 1: Create and Enable a Custom Module for SearchStax Site Search
- Go to
web/modules. - Create a new folder called custom.
- Create a folder called sitesearch in the
web/modules/customfolder. -
Create a new file in this folder called
sitesearch.info.ymlwith the following content:name: SearchStax Site Search description: Custom module for SearchStax Site Search Hosted Search Experience type: module core_version_requirement: ^8 || ^9 || ^10 || ^11 || ^12 - Go to the admin panel of your Drupal site and click Extend.
- Find the Sitesearch module, select it, and click Install.
Step 2: Create a Route and a Controller
- Create a new file called
sitesearch.routing.ymlinweb/modules/custom/sitesearch. -
Define your path and controller class name in this file, with proper indents and case-sensitive names:
sitesearch.search_page: path: '/home/sitesearch' defaults: _controller: '\Drupal\sitesearch\Controller\SitesearchController::searchPage' requirements: _permission: 'access content' - Create a folder called src in
web/modules/custom/sitesearch. - Create a folder called Controller in
web/modules/custom/sitesearch/src. -
Create a controller file with the same name as the one defined in the route (
SitesearchController.php):'sitesearch', ]; } }
Step 3: Create a Twig Template
- Create a module file in
web/modules/custom/sitesearchnamedsitesearch.module. -
You need to implement the
hook_thememethod in this file:[ 'render element' => 'children', 'template' => 'sitesearch', 'path' => $path . '/templates' ], ]; } - Create a templates folder in
web/modules/custom/sitesearch. - Create a file in the templates folder that will host the search page code:
sitesearch.html.twig. -
Enter some demo text in it to test:
Site Search page Hello!
Step 4: Inject Site Search Libraries
- Create a file called
sitesearch.libraries.ymlinweb/modules/custom/sitesearch. -
Define Site Search JS libraries in this file:
sitesearch-app: header: true version: 4.0 js: https://static.searchstax.com/studio-js/v4.1.9/js/feedbackWidget.mjs: { external: true, attributes: { defer: true, async: true } } https://static.searchstax.com/studio-js/v4.1.9/js/search-ux.js: { external: true, attributes: { defer: true, async: true } } css: theme: https://static.searchstax.com/studio-js/v4.1.9/css/search-ux.css: { external: true, attributes: { defer: true, async: true } } https://static.searchstax.com/studio-js/v4.1.9/css/feedbackWidget.css: { external: true, attributes: { defer: true, async: true } } - You can get the latest version number from the Site Search changelog.
-
Go to the
sitesearch.modulefile. Add the following preprocess hook to load the libraries:/** * Implements hook_preprocess_page() */ function sitesearch_preprocess_page(&$variables) { $variables['#attached']['library'][] = 'sitesearch/sitesearch-app'; } - Your folder structure will look like this at this point:
Step 5: Clear the Cache and Test
-
You can either clear the cache through the Drupal admin panel or by running the following commands:
drush cc theme-registry && drush cc router && drush cr - Now, go to the
/home/sitesearchpage, and you'll see the empty page that was created earlier.
Step 6: Get the Template Code from Site Search
Now you can get the search page code from SearchStax Site Search and paste it into the twig template.
- Log into the SearchStax Site Search dashboard.
- Select the Account App.
- Go to Hosted Search Experience under Search UI on the left, and click View.
- Right-click and select View page source.
- Copy everything into the twig file created above.
- Escape the double curly brackets so Twig doesn't treat them as variable syntax:
- Replace all
{{with{{ '{{ - Replace all
}}with}}' }}
- Replace all
-
All the variables will now look like this:
"{{'{{ searchStore.searchTerm }}'}}" - Clear the cache and open the search page.
You now have a fully working search page in Drupal. Customize the page to match your site's design and requirements.
Next Steps
Now that you've imported your Hosted Search Experience, here are some helpful resources:
- Sync your Drupal version with SearchStax: Keep your Drupal version in sync with SearchStax after upgrades
- Drupal Auto-Suggest: Add autocomplete functionality to your search
- Building Drupal Pages with Search UI Kit: Alternative approach using Search UI Kit
- Adding search with the Hosted Search Experience: Learn more about Hosted Search Experience features