You are here

function file_entity_search_execute in File Entity (fieldable files) 7.3

Same name and namespace in other branches
  1. 7.2 file_entity.module \file_entity_search_execute()

Implements hook_search_execute().

File

./file_entity.module, line 592
Extends Drupal file entities to be fieldable and viewable.

Code

function file_entity_search_execute($keys = NULL, $conditions = NULL) {
  global $user;

  // Build matching conditions
  $query = db_select('search_index', 'i', array(
    'target' => 'slave',
  ))
    ->extend('SearchQuery')
    ->extend('PagerDefault');
  $query
    ->join('file_managed', 'fm', 'fm.fid = i.sid');
  $query
    ->searchExpression($keys, 'file');

  // Insert special keywords.
  $query
    ->setOption('type', 'fm.type');
  if ($query
    ->setOption('term', 'ti.tid')) {
    $query
      ->join('taxonomy_index', 'ti', 'fm.fid = ti.fid');
  }

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

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

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

    // Render the file.
    $file = file_load($item->sid);
    $build = file_view($file, 'search_result');
    unset($build['#theme']);
    $file->rendered = drupal_render($build);
    $extra = module_invoke_all('file_entity_search_result', $file);
    $types = file_entity_type_get_names();
    $uri = entity_uri('file', $file);
    $results[] = array(
      'link' => url($uri['path'], array_merge($uri['options'], array(
        'absolute' => TRUE,
      ))),
      'type' => check_plain($types[$file->type]),
      'title' => $file->filename,
      'user' => theme('username', array(
        'account' => user_load($file->uid),
      )),
      'date' => $file->timestamp,
      'file' => $file,
      'extra' => $extra,
      'score' => $item->calculated_score,
      'snippet' => search_excerpt($keys, $file->rendered),
      'language' => function_exists('entity_language') ? entity_language('file', $file) : NULL,
    );
  }
  return $results;
}