You are here

function linkit_taxonomy_linkit_load_plugins in Linkit 7

Same name and namespace in other branches
  1. 6 plugins/linkit_taxonomy/linkit_taxonomy.module \linkit_taxonomy_linkit_load_plugins()

Implements hook_linkit_load_plugins().

File

plugins/linkit_taxonomy/linkit_taxonomy.module, line 12
Extend Linkit with taxonomy links.

Code

function linkit_taxonomy_linkit_load_plugins($string) {
  $matches = array();
  $settings = variable_get('linkit_term', array());

  // Prevent "PHP notice: Undefined variable"
  _linkit_taxonomy_get_default_settings($settings);

  // Build default query
  $query = db_select('taxonomy_term_data', 't')
    ->fields('t', array(
    'name',
    'tid',
  ))
    ->condition('t.name', '%' . db_like($string) . '%', 'LIKE')
    ->addTag('term_access');

  // Get terms
  $result = $query
    ->execute();
  $i = 0;
  foreach ($result as $term) {
    $uri = entity_uri('taxonomy_term', taxonomy_term_load($term->tid));
    $matches['taxonomy'][$i] = array(
      'title' => $term->name,
      'path' => $uri['path'],
      'information' => array(
        'type' => 'Taxonomy',
      ),
    );

    // Add the taxonomy path
    if ($settings['display_settings']['show_parent']) {

      // Possible to find all parents to the root level, for now this isnt really pretty to put in
      // $parents = taxonomy_get_parents_all($term->tid);
      // The API says "Find all parents of a given term ID." but thats not true
      // It is only returning the very next parent
      $parents = taxonomy_get_parents($term->tid);
      if (count($parents)) {
        $current = current($parents);
        $matches['taxonomy'][$i]['information']['parent'] = $current->name;
      }
    }
    $i++;
  }
  return $matches;
}