You are here

function term_search_search_execute in Term Search 7

Implements hook_search_execute().

File

./term_search.module, line 109
Functions to index and search taxonomy terms.

Code

function term_search_search_execute($keys = NULL, $conditions = NULL) {

  // Link principle tables.
  $query = db_select('search_index', 'i', array(
    'target' => 'slave',
  ))
    ->extend('SearchQuery')
    ->extend('PagerDefault');
  $query
    ->join('taxonomy_term_data', 'te', 'te.tid = i.sid');
  $query
    ->searchExpression($keys, 'term');

  // Link to attatched nodes.
  if ($query
    ->setOption('term', 'ti.tid')) {
    $query
      ->join('taxonomy_index', 'ti', 'n.nid = ti.nid');
  }

  // Limit to selected vocabularies.
  $vocabularies = variable_get('term_search_indexed_vocabularies', _term_search_default_vids());
  $query
    ->condition('te.vid', array_values(array_filter($vocabularies)), 'IN');

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

  // Load results.
  $find = $query
    ->limit(10)
    ->execute();
  $results = array();
  foreach ($find as $item) {

    // Build the term body.
    $term = taxonomy_term_load($item->sid);
    $content = taxonomy_term_view($term, 'search_result');
    $term->body = drupal_render($content);

    // Allow other modules to provide additional information.
    $extra = module_invoke_all('term_search_result', $term);
    $uri = entity_uri('taxonomy_term', $term);
    $results[] = array(
      'link' => url($uri['path'], array_merge($uri['options'], array(
        'absolute' => TRUE,
      ))),
      'type' => $term->vocabulary_machine_name,
      'title' => $term->name,
      'tid' => $term->tid,
      'term' => $term,
      'snippet' => search_excerpt($keys, $term->body),
      'extra' => $extra,
    );
  }
  return $results;
}