You are here

protected function IndexOverviewForm::createSearchForPlugin in Search API Autocomplete 8

Creates a new search entity for the given search plugin.

Parameters

\Drupal\search_api_autocomplete\Search\SearchPluginInterface $plugin: The search plugin for which to create a search entity.

true[] $used_ids: The IDs of the searches created so far for this form (to avoid duplicates). IDs are used as array keys, mapping to TRUE.

Return value

\Drupal\search_api_autocomplete\SearchInterface The new search entity.

1 call to IndexOverviewForm::createSearchForPlugin()
IndexOverviewForm::buildForm in src/Form/IndexOverviewForm.php
Form constructor.

File

src/Form/IndexOverviewForm.php, line 224

Class

IndexOverviewForm
Defines the overview of all search autocompletion configurations.

Namespace

Drupal\search_api_autocomplete\Form

Code

protected function createSearchForPlugin(SearchPluginInterface $plugin, array $used_ids) {
  $plugin_id = $plugin
    ->getPluginId();
  $index_id = $plugin
    ->getIndexId();
  $id = $base_id = $plugin
    ->getDerivativeId() ?: $plugin_id;
  $search_storage = $this->entityTypeManager
    ->getStorage('search_api_autocomplete_search');
  $i = 0;
  while (!empty($used_ids[$id]) || $search_storage
    ->load($id)) {
    $id = $base_id . '_' . ++$i;
  }
  $search = Search::create([
    'id' => $id,
    'status' => FALSE,
    'label' => $plugin
      ->label(),
    'index_id' => $index_id,
    'search_settings' => [
      $plugin_id => [],
    ],
    'options' => [],
  ]);

  // Find a supported suggester and set it as the default for the search.
  $suggesters = array_map(function ($suggester_info) {
    return $suggester_info['class'];
  }, $this->suggesterManager
    ->getDefinitions());
  $filter_suggesters = function ($suggester_class) use ($search) {
    return $suggester_class::supportsSearch($search);
  };
  $available_suggesters = array_filter($suggesters, $filter_suggesters);
  if ($available_suggesters) {
    reset($available_suggesters);
    $suggester_id = key($available_suggesters);
    $suggester = $this->pluginHelper
      ->createSuggesterPlugin($search, $suggester_id);
    $search
      ->setSuggesters([
      $suggester_id => $suggester,
    ]);
  }
  return $search;
}