You are here

public function SearchApiSearch::getConfigForm in Fast Autocomplete 8

Gets the configuration form for the search plugin.

Parameters

array $plugin_config: The plugin config array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

Return value

array The configuration form for the search plugin.

Overrides SearchBase::getConfigForm

File

src/Plugin/Search/SearchApiSearch.php, line 99

Class

SearchApiSearch
Provides a Search API search plugin.

Namespace

Drupal\fac\Plugin\Search

Code

public function getConfigForm(array $plugin_config, FormStateInterface $form_state) {
  $input = $form_state
    ->getUserInput();
  $values_index_id = isset($input['plugin']['config']['index']) ? $input['plugin']['config']['index'] : '';
  $config_index_id = isset($plugin_config['index']) ? $plugin_config['index'] : '';
  $index_options = [];
  try {
    $query = $this->entityTypeManager
      ->getStorage('search_api_index')
      ->getQuery();
    $index_ids = $query
      ->execute();
    $indexes = $this->entityTypeManager
      ->getStorage('search_api_index')
      ->loadMultiple($index_ids);
  } catch (InvalidPluginDefinitionException $e) {
    $indexes = [];
  }
  foreach ($indexes as $index) {
    if ($index
      ->status()) {
      $index_options[$index
        ->id()] = $index
        ->label();
    }
  }
  $form['index'] = [
    '#type' => 'select',
    '#options' => $index_options,
    '#title' => $this
      ->t('Select the index to use'),
    '#required' => TRUE,
    '#default_value' => isset($plugin_config['index']) ? $plugin_config['index'] : '',
    '#description' => $this
      ->t('Only active indexes are shown. Missing an index? Check if the index is enabled in the <a href=":href">Search API configuration</a>.', [
      ':href' => '/admin/config/search/search-api',
    ]),
    '#ajax' => [
      'callback' => '::pluginSelection',
      'wrapper' => 'plugin-subform',
      'event' => 'change',
      'effect' => 'fade',
      'progress' => [
        'message' => $this
          ->t('Loading search plugin options...'),
      ],
    ],
  ];
  $text_fields = [];
  $sort_fields = [
    'search_api_relevance' => $this
      ->t('Relevance'),
    'search_api_id' => $this
      ->t('Item ID'),
  ];
  if (!empty($values_index_id) || !empty($config_index_id)) {
    try {

      /* @var \Drupal\search_api\IndexInterface $index */
      $index = $this->entityTypeManager
        ->getStorage('search_api_index')
        ->load(!empty($values_index_id) ? $values_index_id : $config_index_id);
      $index_fields = $index
        ->getFields(TRUE);
      $full_text_fields = $index
        ->getFullTextFields();
      $sortable_types = [
        'string',
        'date',
      ];
      foreach ($index_fields as $index_field) {
        if (in_array($index_field
          ->getFieldIdentifier(), $full_text_fields)) {
          $text_fields[$index_field
            ->getFieldIdentifier()] = $index_field
            ->getLabel();
        }
        if (in_array($index_field
          ->getType(), $sortable_types)) {
          $sort_fields[$index_field
            ->getFieldIdentifier()] = $index_field
            ->getLabel();
        }
      }
    } catch (InvalidPluginDefinitionException $e) {
    }
  }
  $form['text_fields'] = [
    '#type' => 'select',
    '#options' => $text_fields,
    '#title' => $this
      ->t('Full text fields to search through'),
    '#multiple' => TRUE,
    '#default_value' => isset($plugin_config['text_fields']) ? $plugin_config['text_fields'] : 'all',
    '#description' => $this
      ->t('Select the full text fields to search through. No selection will result in searching through all fields.'),
  ];
  $form['sort_field'] = [
    '#type' => 'select',
    '#options' => $sort_fields,
    '#title' => $this
      ->t('Select the sort field'),
    '#required' => TRUE,
    '#default_value' => isset($plugin_config['sort_field']) ? $plugin_config['sort_field'] : 'search_api_relevance',
    '#description' => $this
      ->t('Select the field that the result is sorted by.'),
  ];
  $form['sort_direction'] = [
    '#type' => 'select',
    '#options' => [
      'ASC' => $this
        ->t('Ascending'),
      'DESC' => $this
        ->t('Descending'),
    ],
    '#title' => $this
      ->t('Select the sort direction'),
    '#required' => TRUE,
    '#default_value' => isset($plugin_config['sort_direction']) ? $plugin_config['sort_direction'] : 'DESC',
    '#description' => $this
      ->t('Select the sort direction.'),
  ];
  $form['language_filter'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Filter by language'),
    '#default_value' => isset($plugin_config['language_filter']) ? $plugin_config['language_filter'] : '',
    '#description' => $this
      ->t('Check this option if you want the results to be filtered by language'),
  ];
  $form['langcode_includes'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('No specific language'),
    '#options' => [
      LanguageInterface::LANGCODE_NOT_APPLICABLE => $this
        ->t('Include "language not applicable"'),
      LanguageInterface::LANGCODE_NOT_SPECIFIED => $this
        ->t('Include "Language not specified"'),
    ],
    '#default_value' => isset($plugin_config['langcode_includes']) ? $plugin_config['langcode_includes'] : [],
    '#states' => [
      'invisible' => [
        ':input[name="plugin[config][language_filter]"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];
  return $form;
}