Make SearchFactory a service

Created on 22 January 2024, 11 months ago

Problem/Motivation

Currently, SearchFactory is implemented as a class with two static methods—::search and ::parseResult—which are only called by SearchApiElasticsearchBackend. If it were instead implemented as a service (with its methods becoming non-static public), this would allow other modules to override the factory to customize the search logic as needed.

For example, a custom module author might want to extend SearchBuilder in order to support additional ElasticSearch features. If SearchFactory were a service (elasticsearch_connector.search_factory) they could achieve this like so:

class MyModuleServiceProvider extends ServiceProviderBase {
  public function alter(ContainerBuilder $container) {
    $definition = $container->getDefinition('elasticsearch_connector.search_factory');
    $definition->setClass('Drupal\my_module\MyModuleSearchFactory');
  }
}

class MyModuleSearchFactory extends SearchFactory {
  public function search(QueryInterface $query) {
    $builder = new MyModuleSearchBuilder($query);
    return $builder->build();
  }
}

class MyModuleSearchBuilder extends SearchBuilder {
  public function build() {
    // Do cool stuff here.
  }
}

API changes

Calls to SearchFactory::search and SearchFactory::parseResult would need to be changed to \Drupal::service('elasticsearch_connector.search_factory')->search and \Drupal::service('elasticsearch_connector.search_factory')->parseResult.

Feature request
Status

Needs review

Version

7.0

Component

Elasticsearch Connector Search API

Created by

🇺🇸United States muriqui

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024