You are here

linkit_taxonomy.module in Linkit 6

Same filename and directory in other branches
  1. 7 plugins/linkit_taxonomy/linkit_taxonomy.module

Extend Linkit with taxonomy links.

File

plugins/linkit_taxonomy/linkit_taxonomy.module
View source
<?php

/**
 * @file
 * Extend Linkit with taxonomy links.
 */

/**
 * Implementation of hook_linkit_load_plugins().
 */
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;
}

/**
 * Implementation of hook_linkit_get_search_styled_link().
 */
function linkit_taxonomy_linkit_get_search_styled_link($string) {

  // Term links created with Linkit will always begin with "internal:"
  if (strpos($string, 'internal:') === FALSE) {
    return;
  }

  // Check to see that the link really is a term link
  $splitted_string = explode('/', str_replace('internal:', '', $string));
  if (!is_numeric($splitted_string[count($splitted_string) - 1])) {
    return;
  }

  // Try to get a term object
  $term = taxonomy_get_term($splitted_string[count($splitted_string) - 1]);

  // taxonomy_get_term is returns an object
  if (!is_object($term)) {
    return;
  }

  // Ok, this looks like a term, get the path
  $path = taxonomy_term_path($term);

  // If $path is the same as the provided $string, we have a match.
  if ($path == str_replace('internal:', '', $string)) {
    return check_plain($term->name) . ' [path:internal:' . taxonomy_term_path($term) . ']';
  }
}

/**
 * Implementation of hook_linkit_info_plugins().
 *
 * This is used by linkit_permissions
 */
function linkit_taxonomy_linkit_info_plugins() {
  $return['linkit_taxonomy'] = array(
    'type' => 'taxonomy',
  );
  return $return;
}

/**
 * Prevent "PHP notice: Undefined variable" by merging the settings
 * with the defule values
 */
function _linkit_taxonomy_get_default_settings(&$settings) {

  // Merge settings with the default settings
  $settings += array(
    'display_settings' => array(
      'show_parent' => 0,
    ),
  );
}

Functions

Namesort descending Description
linkit_taxonomy_linkit_get_search_styled_link Implementation of hook_linkit_get_search_styled_link().
linkit_taxonomy_linkit_info_plugins Implementation of hook_linkit_info_plugins().
linkit_taxonomy_linkit_load_plugins Implementation of hook_linkit_load_plugins().
_linkit_taxonomy_get_default_settings Prevent "PHP notice: Undefined variable" by merging the settings with the defule values