You are here

function search_api_glossary_form_facetapi_facet_display_form_alter in Search API AZ Glossary 7

Same name and namespace in other branches
  1. 7.3 search_api_glossary.module \search_api_glossary_form_facetapi_facet_display_form_alter()
  2. 7.2 search_api_glossary.module \search_api_glossary_form_facetapi_facet_display_form_alter()

Add search_api_glossary settings to facet configuration.

Implements hook_form_BASE_FORM_ID_alter().

File

./search_api_glossary.module, line 23
Search api glossary module file.

Code

function search_api_glossary_form_facetapi_facet_display_form_alter(&$form, &$form_state, $form_id) {
  $adapter = $form['#facetapi']['adapter'];
  $facet = $form['#facetapi']['facet'];
  $fields = _search_api_glossary_get_field();

  // Do not alter other facetapi display forms.
  if (!in_array($facet['field'], $fields)) {
    return;
  }
  $searcher = $adapter
    ->getSearcher();
  list($adapter_key, $index_key) = explode('@', $searcher, 2);

  // We know how to handle only search_api at the moment.
  if ($adapter_key != 'search_api') {
    return;
  }
  if ($index = search_api_index_load($index_key)) {
    $facet_settings = $adapter
      ->getFacetSettingsGlobal($facet);
    $index_fields = $index
      ->getFields();
    $index_field_names = $index
      ->server()
      ->getFieldNames($index);
    $options = array(
      '' => $index_fields[$facet['field']]['name'],
    );
    foreach ($index_fields as $key => $text_field) {
      if (in_array($text_field['type'], array(
        'text',
        'string',
      )) && $key != $facet['field']) {
        $options[$index_field_names[$key]] = $text_field['name'];
      }
    }
    $field_key = $facet['field'] . '_field';
    $field_value = !empty($facet_settings->settings[$field_key]) ? $facet_settings->settings[$field_key] : '';
    $form['global'][$field_key] = array(
      '#type' => 'select',
      '#title' => t('Source field'),
      '#default_value' => $field_value,
      '#options' => $options,
      '#description' => t('Select the source field for glossary sorting. This is useful if you have multiple fields, eg: Display Title Field and Sort Title Field.'),
    );
  }
}