You are here

function elasticsearch_connector_autocomp_form_search_api_index_fields_alter in Elasticsearch Connector Autocomplete 8

Implements hook_form_FORM_ID_alter().

File

./elasticsearch_connector_autocomp.module, line 190
Contains elasticsearch_connector_autocomp.module.

Code

function elasticsearch_connector_autocomp_form_search_api_index_fields_alter(&$form, FormStateInterface $form_state, $form_id) {

  /** @var \Drupal\search_api\IndexInterface $index_entity */
  $index_entity = $form_state
    ->getFormObject()
    ->getEntity();
  $ngram_index_analyzer_enabled = $index_entity
    ->getThirdPartySetting('elasticsearch_connector', 'ngram_filter_enabled', FALSE);
  $types = $index_entity
    ->getEntityTypes();
  foreach ($types as $entity_key => $entity_type) {
    if (empty($form[$entity_key]['fields'])) {
      continue;
    }
    foreach ($form[$entity_key]['fields'] as $field_id => &$field) {
      if ($ngram_index_analyzer_enabled) {

        // Show the boost select box if this data type is selected.
        $field['boost']['#states']['visible'][':input[name="fields[' . $field_id . '][type]"]'][] = [
          'value' => 'text_ngram',
        ];
      }
      else {

        // Hide this data type if ngrams are not enabled on this index.
        unset($field['type']['#options']['text_ngram']);
      }
    }
  }
}