You are here

search_api_glossary.module in Search API AZ Glossary 7

Search api glossary module file.

File

search_api_glossary.module
View source
<?php

/**
 * @file
 * Search api glossary module file.
 */

/**
 * Helper function to return list of fields.
 */
function _search_api_glossary_get_field() {
  $fields = array(
    'title_az_glossary',
  );
  return $fields;
}

/**
 * Add search_api_glossary settings to facet configuration.
 *
 * Implements hook_form_BASE_FORM_ID_alter().
 */
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.'),
    );
  }
}

/**
 * Alter Solr documents before they are sent to Solr for indexing.
 *
 * @param array $documents
 *   An array of SearchApiSolrDocument objects ready to be indexed, generated
 *   from $items array.
 * @param SearchApiIndex $index
 *   The search index for which items are being indexed.
 * @param array $items
 *   An array of items being indexed.
 */
function search_api_glossary_search_api_solr_documents_alter(array &$documents, SearchApiIndex $index, array $items) {
  $searcher = 'search_api@' . $index->machine_name;
  $fields = _search_api_glossary_get_field();
  $field_names = $index
    ->server()
    ->getFieldNames($index);
  foreach ($fields as $field) {
    if ($facet = facetapi_facet_load($field, $searcher)) {
      $adapter = facetapi_adapter_load($searcher);
      $facet_settings = $adapter
        ->getFacetSettingsGlobal($facet);
      $field_key = $facet['field'] . '_field';
      if (!empty($facet_settings->settings[$field_key])) {
        $source_field_key = $facet_settings->settings[$field_key];
        foreach ($documents as $document) {
          $source_field = $document
            ->getField($source_field_key);
          $value = '';
          if (is_string($source_field['value']) && drupal_strlen($source_field['value']) > 0) {
            $first_letter = strtoupper($source_field['value'][0]);
            $value = search_api_glossary_getter_helper($first_letter);
          }
          $document
            ->setField($field_names[$field], $value);
        }
      }
    }
  }
}

/**
 * Implements hook_entity_property_info().
 *
 * @see entity_entity_property_info()
 */
function search_api_glossary_entity_property_info() {
  $info = array();

  // Add meta-data about the basic node properties.
  $properties =& $info['node']['properties'];
  $properties['title_az_glossary'] = array(
    'label' => t("Title A-Z Glossary"),
    'type' => 'text',
    'description' => t("The A-Z Glossary from title of the node."),
    'getter callback' => 'search_api_glossary_title_getter_callback',
    'schema field' => 'az_title',
    'sanitized' => TRUE,
  );
  return $info;
}

/**
 * Getter callback for title_az_glossary property.
 */
function search_api_glossary_title_getter_callback($node, $name) {

  // @TODO: remove hardcoded node title field and expose this as a setting.
  $first_letter = strtoupper($node->title[0]);
  $key = search_api_glossary_getter_helper($first_letter);
  return $key;
}

/**
 * Getter Helper for Alpha Numeric Keys.
 */
function search_api_glossary_getter_helper($first_letter) {

  // Is it alpha?
  if (ctype_alpha($first_letter)) {
    $key = $first_letter;
  }
  elseif (ctype_digit($first_letter)) {
    $key = "0-9";
  }
  else {
    $key = "#";
  }
  return $key;
}

/**
 * Implements hook_facetapi_facet_info_alter().
 */
function search_api_glossary_facetapi_facet_info_alter(array &$facet_info, array $searcher_info) {
  if ($searcher_info['adapter'] == 'search_api' && isset($searcher_info['types']['node'])) {
    $facet_info['title_az_glossary']['alter callbacks'][] = 'search_api_glossary_facet_strip_facet_count';
  }
}

/**
 * Facet API "alter callback".
 *
 * Pads the glossary when there are no results
 * displayed when the facet is rendered.
 */
function search_api_glossary_facet_strip_facet_count(&$build, FacetapiAdapter $adapter, array $facet) {
  $glossary_array = range('A', 'Z');
  $glossary_array[] = "#";
  $glossary_array[] = "0-9";
  foreach ($glossary_array as $key) {
    if (!array_key_exists($key, $build)) {
      $build[$key]['#count'] = 0;
    }
  }
}

/**
 * Implements hook_theme_registry_alter().
 */
function search_api_glossary_theme_registry_alter(&$theme_registry) {
  $path = drupal_get_path('module', 'search_api_glossary');

  // Override the content type summary info.
  if (isset($theme_registry['facetapi_link_inactive'])) {
    $theme_registry['facetapi_link_inactive']['includes'][] = $path . '/search_api_glossary.theme.inc';
    $theme_registry['facetapi_link_inactive']['function'] = 'theme_search_api_glossary_facetapi_link_inactive';
    $theme_registry['facetapi_link_active']['includes'][] = $path . '/search_api_glossary.theme.inc';
    $theme_registry['facetapi_link_active']['function'] = 'theme_search_api_glossary_facetapi_link_active';
    $theme_registry['facetapi_count']['includes'][] = $path . '/search_api_glossary.theme.inc';
    $theme_registry['facetapi_count']['function'] = 'theme_search_api_glossary_facetapi_count';
  }
}

/**
 * Implements hook_facetapi_sort_info().
 */
function search_api_glossary_facetapi_sort_info() {
  $sorts = array();
  $sorts['glossary_az'] = array(
    'label' => t('Glossary A-Z'),
    'callback' => 'search_api_glossary_sort_glossary',
    'description' => t('Sort by Glossary A-Z then 0-9 and then #.'),
    'weight' => -40,
  );
  return $sorts;
}

/**
 * Sorts by A-Z then 0-9 and then # weight.
 */
function search_api_glossary_sort_glossary(array $a, array $b) {
  $a_value = isset($a['#indexed_value']) ? $a['#indexed_value'] : '';
  $b_value = isset($b['#indexed_value']) ? $b['#indexed_value'] : '';
  if ($a_value == $b_value) {
    return 0;
  }
  elseif (ctype_alpha($a_value) && ctype_alpha($b_value)) {
    return $a_value < $b_value ? -1 : 1;
  }
  elseif (($a_value == "#" || $a_value == "0-9") && ctype_alpha($b_value)) {
    return 1;
  }
  elseif (ctype_alpha($a_value) && ($b_value == "#" || $b_value == "0-9")) {
    return -1;
  }
  elseif ($a_value == "#" && $b_value == "0-9") {
    return 1;
  }
  elseif ($b_value == "0-9" && $a_value == "#") {
    return -1;
  }
}

Functions

Namesort descending Description
search_api_glossary_entity_property_info Implements hook_entity_property_info().
search_api_glossary_facetapi_facet_info_alter Implements hook_facetapi_facet_info_alter().
search_api_glossary_facetapi_sort_info Implements hook_facetapi_sort_info().
search_api_glossary_facet_strip_facet_count Facet API "alter callback".
search_api_glossary_form_facetapi_facet_display_form_alter Add search_api_glossary settings to facet configuration.
search_api_glossary_getter_helper Getter Helper for Alpha Numeric Keys.
search_api_glossary_search_api_solr_documents_alter Alter Solr documents before they are sent to Solr for indexing.
search_api_glossary_sort_glossary Sorts by A-Z then 0-9 and then # weight.
search_api_glossary_theme_registry_alter Implements hook_theme_registry_alter().
search_api_glossary_title_getter_callback Getter callback for title_az_glossary property.
_search_api_glossary_get_field Helper function to return list of fields.