cms/drupal/modules/taxonomy/taxonomy.tokens.inc
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @file
       
     5  * Builds placeholder replacement tokens for taxonomy terms and vocabularies.
       
     6  */
       
     7 
       
     8 /**
       
     9  * Implements hook_token_info().
       
    10  */
       
    11 function taxonomy_token_info() {
       
    12   $types['term'] = array(
       
    13     'name' => t("Taxonomy terms"),
       
    14     'description' => t("Tokens related to taxonomy terms."),
       
    15     'needs-data' => 'term',
       
    16   );
       
    17   $types['vocabulary'] = array(
       
    18     'name' => t("Vocabularies"),
       
    19     'description' => t("Tokens related to taxonomy vocabularies."),
       
    20     'needs-data' => 'vocabulary',
       
    21   );
       
    22 
       
    23   // Taxonomy term related variables.
       
    24   $term['tid'] = array(
       
    25     'name' => t("Term ID"),
       
    26     'description' => t("The unique ID of the taxonomy term."),
       
    27   );
       
    28   $term['name'] = array(
       
    29     'name' => t("Name"),
       
    30     'description' => t("The name of the taxonomy term."),
       
    31   );
       
    32   $term['description'] = array(
       
    33     'name' => t("Description"),
       
    34     'description' => t("The optional description of the taxonomy term."),
       
    35   );
       
    36   $term['node-count'] = array(
       
    37     'name' => t("Node count"),
       
    38     'description' => t("The number of nodes tagged with the taxonomy term."),
       
    39   );
       
    40   $term['url'] = array(
       
    41     'name' => t("URL"),
       
    42     'description' => t("The URL of the taxonomy term."),
       
    43   );
       
    44 
       
    45   // Taxonomy vocabulary related variables.
       
    46   $vocabulary['vid'] = array(
       
    47     'name' => t("Vocabulary ID"),
       
    48     'description' => t("The unique ID of the taxonomy vocabulary."),
       
    49   );
       
    50   $vocabulary['name'] = array(
       
    51     'name' => t("Name"),
       
    52     'description' => t("The name of the taxonomy vocabulary."),
       
    53   );
       
    54   $vocabulary['description'] = array(
       
    55     'name' => t("Description"),
       
    56     'description' => t("The optional description of the taxonomy vocabulary."),
       
    57   );
       
    58   $vocabulary['node-count'] = array(
       
    59     'name' => t("Node count"),
       
    60     'description' => t("The number of nodes tagged with terms belonging to the taxonomy vocabulary."),
       
    61   );
       
    62   $vocabulary['term-count'] = array(
       
    63     'name' => t("Term count"),
       
    64     'description' => t("The number of terms belonging to the taxonomy vocabulary."),
       
    65   );
       
    66 
       
    67   // Chained tokens for taxonomies
       
    68   $term['vocabulary'] = array(
       
    69     'name' => t("Vocabulary"),
       
    70     'description' => t("The vocabulary the taxonomy term belongs to."),
       
    71     'type' => 'vocabulary',
       
    72   );
       
    73   $term['parent'] = array(
       
    74     'name' => t("Parent term"),
       
    75     'description' => t("The parent term of the taxonomy term, if one exists."),
       
    76     'type' => 'term',
       
    77   );
       
    78 
       
    79   return array(
       
    80     'types' => $types,
       
    81     'tokens' => array(
       
    82       'term' => $term,
       
    83       'vocabulary' => $vocabulary,
       
    84     ),
       
    85   );
       
    86 }
       
    87 
       
    88 /**
       
    89  * Implements hook_tokens().
       
    90  */
       
    91 function taxonomy_tokens($type, $tokens, array $data = array(), array $options = array()) {
       
    92   $replacements = array();
       
    93   $sanitize = !empty($options['sanitize']);
       
    94 
       
    95   if ($type == 'term' && !empty($data['term'])) {
       
    96     $term = $data['term'];
       
    97 
       
    98     foreach ($tokens as $name => $original) {
       
    99       switch ($name) {
       
   100         case 'tid':
       
   101           $replacements[$original] = $term->tid;
       
   102           break;
       
   103 
       
   104         case 'name':
       
   105           $replacements[$original] = $sanitize ? check_plain($term->name) : $term->name;
       
   106           break;
       
   107 
       
   108         case 'description':
       
   109           $replacements[$original] = $sanitize ? check_markup($term->description, $term->format, '', TRUE) : $term->description;
       
   110           break;
       
   111 
       
   112         case 'url':
       
   113           $uri = entity_uri('taxonomy_term', $term);
       
   114           $replacements[$original] = url($uri['path'], array_merge($uri['options'], array('absolute' => TRUE)));
       
   115           break;
       
   116 
       
   117         case 'node-count':
       
   118           $query = db_select('taxonomy_index');
       
   119           $query->condition('tid', $term->tid);
       
   120           $query->addTag('term_node_count');
       
   121           $count = $query->countQuery()->execute()->fetchField();
       
   122           $replacements[$original] = $count;
       
   123           break;
       
   124 
       
   125         case 'vocabulary':
       
   126           $vocabulary = taxonomy_vocabulary_load($term->vid);
       
   127           $replacements[$original] = check_plain($vocabulary->name);
       
   128           break;
       
   129 
       
   130         case 'parent':
       
   131           if ($parents = taxonomy_get_parents($term->tid)) {
       
   132             $parent = array_pop($parents);
       
   133             $replacements[$original] = check_plain($parent->name);
       
   134           }
       
   135           break;
       
   136       }
       
   137     }
       
   138 
       
   139     if ($vocabulary_tokens = token_find_with_prefix($tokens, 'vocabulary')) {
       
   140       $vocabulary = taxonomy_vocabulary_load($term->vid);
       
   141       $replacements += token_generate('vocabulary', $vocabulary_tokens, array('vocabulary' => $vocabulary), $options);
       
   142     }
       
   143 
       
   144     if (($vocabulary_tokens = token_find_with_prefix($tokens, 'parent')) && $parents = taxonomy_get_parents($term->tid)) {
       
   145       $parent = array_pop($parents);
       
   146       $replacements += token_generate('term', $vocabulary_tokens, array('term' => $parent), $options);
       
   147     }
       
   148   }
       
   149 
       
   150   elseif ($type == 'vocabulary' && !empty($data['vocabulary'])) {
       
   151     $vocabulary = $data['vocabulary'];
       
   152 
       
   153     foreach ($tokens as $name => $original) {
       
   154       switch ($name) {
       
   155         case 'vid':
       
   156           $replacements[$original] = $vocabulary->vid;
       
   157           break;
       
   158 
       
   159         case 'name':
       
   160           $replacements[$original] = $sanitize ? check_plain($vocabulary->name) : $vocabulary->name;
       
   161           break;
       
   162 
       
   163         case 'description':
       
   164           $replacements[$original] = $sanitize ? filter_xss($vocabulary->description) : $vocabulary->description;
       
   165           break;
       
   166 
       
   167         case 'term-count':
       
   168           $query = db_select('taxonomy_term_data');
       
   169           $query->condition('vid', $vocabulary->vid);
       
   170           $query->addTag('vocabulary_term_count');
       
   171           $count = $query->countQuery()->execute()->fetchField();
       
   172           $replacements[$original] = $count;
       
   173           break;
       
   174 
       
   175         case 'node-count':
       
   176           $query = db_select('taxonomy_index', 'ti');
       
   177           $query->addExpression('COUNT(DISTINCT ti.nid)');
       
   178           $query->leftJoin('taxonomy_term_data', 'td', 'ti.tid = td.tid');
       
   179           $query->condition('td.vid', $vocabulary->vid);
       
   180           $query->addTag('vocabulary_node_count');
       
   181           $count = $query->execute()->fetchField();
       
   182           $replacements[$original] = $count;
       
   183           break;
       
   184       }
       
   185     }
       
   186   }
       
   187 
       
   188   return $replacements;
       
   189 }