You are here

function linkit_taxonomy_linkit_load_plugins in Linkit 6

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

Implementation of hook_linkit_load_plugins().

File

plugins/linkit_taxonomy/linkit_taxonomy.module, line 11
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);
  $fields = array(
    't.name',
    't.tid',
    't.vid',
    't.description',
    't.weight',
  );

  // default fields
  $from = array(
    '{term_data} t',
  );

  // default from
  $where = array();

  // default where
  // Prebuild the SQL query
  $sql = array();
  $sql[] = "SELECT %s";
  $sql[] = "FROM " . implode(" ", $from);
  $sql[] = "WHERE LOWER(t.name) LIKE LOWER('%%%s%%') %s";

  // Get terms
  $result = db_query(db_rewrite_sql(implode(" ", $sql), 't', 'tid'), implode(",", $fields), $string, implode(" ", $where));
  $i = 0;
  while ($term = db_fetch_object($result)) {
    $matches['taxonomy'][$i] = array(
      'title' => $term->name,
      'path' => 'internal:' . taxonomy_term_path($term),
      '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;
}