You are here

search_facetapi.module in Faceted Navigation for Search 7

Integrates the core Search module with the Facet API project.

File

search_facetapi.module
View source
<?php

/**
 * @file
 * Integrates the core Search module with the Facet API project.
 */

/**
 * Implements hook_facetapi_searcher_info().
 */
function search_facetapi_facetapi_searcher_info() {
  $info = array();
  $info['search'] = array(
    'label' => t('Search'),
    'adapter' => 'search',
    'path' => 'admin/config/search/settings',
    'supports facet missing' => FALSE,
  );
  return $info;
}

/**
 * Implements hook_menu_alter().
 */
function search_facetapi_menu_alter(&$items) {
  $items['admin/config/search/settings/index'] = array(
    'title' => 'Search settings',
    'weight' => -10,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
}

/**
 * Implements hook_form_alter().
 */
function search_facetapi_form_facetapi_realm_settings_form_alter(&$form, &$form_state) {
  $adapter = $form['#facetapi']['adapter'];
  if ('search' == $adapter
    ->getId()) {
    $active = variable_get('search_active_modules', array(
      'node',
      'user',
    ));
    if (empty($active['search_facetapi'])) {
      $message = t('<em>Faceted Navigation for Search</em> is not set as an active search module in the <a href="@search-settings-url">Search Settings</a> form and is the search page the facets are displayed on.', array(
        '@search-settings-url' => url('admin/config/search/settings'),
      ));
      drupal_set_message($message, 'warning');
    }
  }
}

/**
 * Implements hook_facetapi_facet_info_alter().
 */
function search_facetapi_facetapi_facet_info_alter(array &$facet_info, array $searcher_info) {
  if ('search' == $searcher_info['adapter'] && isset($searcher_info['types']['node'])) {
    $facet_info['bundle']['field'] = 'type';
  }
}

/**
 * Implements hook_search_info().
 */
function search_facetapi_search_info() {
  return array(
    'title' => 'Site content',
    'path' => 'content',
  );
}

/**
 * Implements hook_search_access().
 */
function search_facetapi_search_access() {
  return user_access('access content');
}

/**
 * Implements hook_update_index().
 */
function search_facetapi_update_index() {

  // Piggy-backs off the node index. Invokes the node module's update hook if it
  // is not an active search module.
  $active = variable_get('search_active_modules', array(
    'node',
    'user',
  ));
  if (empty($active['node'])) {
    node_update_index();
  }
}

/**
 * Implements hook_search_status().
 */
function search_facetapi_search_status() {

  // Piggy-backs off the node index. Invokes the node module's update hook if it
  // is not an active search module.
  $active = variable_get('search_active_modules', array(
    'node',
    'user',
  ));
  if (empty($active['node'])) {
    return node_search_status();
  }
}

/**
 * Implements hook_search_execute().
 */
function search_facetapi_search_execute($keys = NULL, $conditions = NULL) {

  // Build matching conditions.
  $query = db_select('search_index', 'i', array(
    'target' => 'slave',
  ))
    ->extend('SearchQuery')
    ->extend('PagerDefault');
  $query
    ->join('node', 'n', 'n.nid = i.sid');
  $query
    ->condition('n.status', 1)
    ->addTag('node_access')
    ->searchExpression($keys, 'node');

  // Ensures the adapter is valid.
  if (!($adapter = facetapi_adapter_load('search'))) {
    return array();
  }

  // Sets search keys and adds active filters.
  $adapter
    ->setSearchKeys($keys);
  $adapter
    ->addActiveFilters($query);

  // Only continue if the first pass query matches.
  $adapter->hasMatches = $query
    ->executeFirstPass();
  if (!$adapter->hasMatches) {
    return array();
  }

  // Add the ranking expressions.
  _node_rankings($query);

  // Executes results, gets and stores the result count.
  global $pager_total_items;
  $element = PagerDefault::$maxElement;
  $find = $query
    ->limit(10)
    ->execute();
  $adapter
    ->setResultCount($pager_total_items[$element]);

  // Builds result rows.
  $results = array();
  foreach ($find as $item) {

    // Render the node.
    $node = node_load($item->sid);
    $build = node_view($node, 'search_result');
    unset($build['#theme']);
    $node->rendered = drupal_render($build);

    // Fetch comments for snippet.
    $node->rendered .= ' ' . module_invoke('comment', 'node_update_index', $node);
    $extra = module_invoke_all('node_search_result', $node);
    $uri = entity_uri('node', $node);
    $results[] = array(
      'link' => url($uri['path'], array_merge($uri['options'], array(
        'absolute' => TRUE,
      ))),
      'type' => check_plain(node_type_get_name($node)),
      'title' => $node->title,
      'user' => theme('username', array(
        'account' => $node,
      )),
      'date' => $node->changed,
      'node' => $node,
      'extra' => $extra,
      'score' => $item->calculated_score,
      'snippet' => search_excerpt($keys, $node->rendered),
      'language' => $node->language,
    );
  }
  return $results;
}