Create Search Server Using Drupal 8 Configuration Import in Custom Module

Last updated on
24 March 2017

Drupal 8 allows modules to provide default configuration (or optional configuration) as a part of custom modules.

Several resources on this topic include:
https://www.drupal.org/node/2087879
https://www.drupal.org/node/2116839
https://www.drupal.org/documentation/administer/config
https://www.drupal.org/node/2703711

For Search API, it is possible to programmatically define one or more components (e.g. search servers, indexes, etc.) on module installation. This page will provide examples of how to accomplish this.

There are only two required files in a custom Drupal 8 module to define a server using the config/install method:
YOURMODULE.info.yml
config/install/search_api.server.YOURSEVER.yml

The .info.yml file can be fairly standard, in this example we are creating a solr server, so both search_api and search_api_solr are provided:

name: 'Search Server Provider'
type: module
description: 'Insert configuration for a default solr server.'
core: 8.x
package: Search
version: 8.x-1.00
dependencies:
  - search_api
  - search_api_solr

Then, you need to setup the configuration to be imported. An example would be:

langcode: en
status: true
id: solr_server
name: 'SOLR Server'
description: 'Default solr server created by the Search Provider module'
backend: search_api_solr
backend_config:
  server_description: ''
  clean_ids: true
  site_hash: true
  scheme: http
  host: localhost
  port: '8080'
  path: /solr
  http_user: ''
  http_pass: ''
  excerpt: false
  retrieve_data: false
  highlight_data: false
  skip_schema_check: false
  solr_version: ''
  http_method: AUTO
  autocorrect_spell: false
  autocorrect_suggest_words: false

To easily generate this file, first define your search server by hand in the Drupal 8 UI. Then, visit the configuration manager in Drupal 8 via (admin/config/development/configuration) and export the single configuration for your search server.

Search API Server Configuration Export

Note the name below the configuration box (in the screenshot, it's search_api.server.dummy.yml). You must follow this naming pattern for your module to properly import the configuration during installation. Replace dummy with the name of your server.

Help improve this page

Page status: No known problems

You can: