source: sipes/modules_contrib/token/token_node.inc @ c43ea01

stableversion-3.0
Last change on this file since c43ea01 was d5990f8, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se actualizo el modulo

  • Propiedad mode establecida a 100755
File size: 13.7 KB
Línea 
1<?php
2
3/**
4 * @file
5 * Implementations of token module hooks for the core node and book modules.
6 *
7 * The token module requires specific hooks to be added to modules
8 * so that those modules can return data about their objects to the
9 * token API.  Until and unless token becomes a part of core, the
10 * implementations of the token hooks for core modules are provided
11 * in the token module itself.
12 *
13 * @ingroup token
14 */
15
16/**
17 * Implements hook_token_list() on behalf of node.module.
18 */
19function node_token_list($type = 'all') {
20  $tokens = array();
21
22  if ($type == 'node' || $type == 'all') {
23    $tokens['node']['nid']             = t('The unique ID of the content item, or "node".');
24    $tokens['node']['type']            = t('The type of the node.');
25    $tokens['node']['type-name']       = t('The human-readable name of the node type.');
26    $tokens['node']['language']        = t('The language the node is written in.');
27    $tokens['node']['title']           = t('The title of the node.');
28    $tokens['node']['title-raw']       = t('The title of the node.');
29    $tokens['node']['node-path']       = t('The URL alias of the node.');
30    $tokens['node']['node-path-raw']   = t('The URL alias of the node.');
31    $tokens['node']['node-url']        = t('The URL of the node.');
32
33    $tokens['node']['author-uid']      = t("The unique ID of the author of the node.");
34    $tokens['node']['author-name']     = t("The login name of the author of the node.");
35    $tokens['node']['author-name-raw'] = t("The login name of the author of the node.");
36    $tokens['node']['author-mail']     = t("The email address of the author of the node.");
37    $tokens['node']['author-mail-raw'] = t("The email address of the author of the node.");
38
39    $tokens['node']['log']     = t('The explanation of the most recent changes made to the node.');
40    $tokens['node']['log-raw'] = t('The explanation of the most recent changes made to the node.');
41
42    $tokens['node'] += token_get_date_token_info(t('Node creation'));
43    $tokens['node'] += token_get_date_token_info(t('Node modification'), 'mod-');
44
45    if (module_exists('comment')) {
46      $tokens['node']['node_comment_count']   = t("The number of comments posted on a node.");
47      $tokens['node']['unread_comment_count'] = t("The number of comments posted on a node since the reader last viewed it.");
48    }
49
50    if (module_exists('taxonomy')) {
51      $tokens['node']['term']            = t("Name of top taxonomy term");
52      $tokens['node']['term-raw']        = t("Unfiltered name of top taxonomy term.");
53      $tokens['node']['term-id']         = t("ID of top taxonomy term");
54      $tokens['node']['vocab']           = t("Name of top term's vocabulary");
55      $tokens['node']['vocab-raw']       = t("Unfiltered name of top term's vocabulary.");
56      $tokens['node']['vocab-id']        = t("ID of top term's vocabulary");
57      // Temporarily disabled -- see notes in node_token_values.
58      // $tokens['node']['catpath']        = t("Full taxonomy tree for the topmost term");
59    }
60  }
61
62  return $tokens;
63}
64
65/**
66 * Implements hook_token_values() on behalf of node.module.
67 */
68function node_token_values($type, $object = NULL, $options = array()) {
69  $values = array();
70
71  if ($type == 'node' && !empty($object)) {
72    $node = $object;
73    $account = db_fetch_object(db_query("SELECT name, mail FROM {users} WHERE uid = %d", $node->uid));
74
75    // Adjust for the anonymous user name.
76    if (!$node->uid && !$account->name) {
77      $account->name = variable_get('anonymous', t('Anonymous'));
78    }
79
80    $values['nid']                = $node->nid;
81    $values['type']               = $node->type;
82    $values['type-name']          = node_get_types('name', $node->type);
83    $values['language']           = check_plain($node->language);
84    $values['title']              = check_plain($node->title);
85    $values['title-raw']          = $node->title;
86    $values['node-path-raw']      = drupal_get_path_alias('node/'. $node->nid);
87    $values['node-path']          = check_plain($values['node-path-raw']);
88    $values['node-url']           = url('node/' . $node->nid, array('absolute' => TRUE));
89    $values['author-uid']         = $node->uid;
90    $values['author-name']        = check_plain($account->name);
91    $values['author-name-raw']    = $account->name;
92    $values['author-mail']        = check_plain($account->mail);
93    $values['author-mail-raw']    = $account->mail;
94
95    $values['log-raw']            = isset($node->log) ? $node->log : '';
96    $values['log']                = filter_xss($values['log-raw']);
97
98    if (module_exists('comment')) {
99      $values['node_comment_count'] = isset($node->comment_count) ? $node->comment_count : 0;
100      $values['unread_comment_count'] = comment_num_new($node->nid);
101    }
102    else {
103      $values['node_comment_count'] = 0;
104      $values['unread_comment_count'] = 0;
105    }
106
107    if (isset($node->created)) {
108      $values += token_get_date_token_values($node->created, '');
109    }
110
111    if (isset($node->changed)) {
112      $values += token_get_date_token_values($node->changed, 'mod-');
113    }
114
115    // And now taxonomy, which is a bit more work. This code is adapted from
116    // pathauto's handling code; it's intended for compatibility with it.
117    if (module_exists('taxonomy') && !empty($node->taxonomy) && is_array($node->taxonomy)) {
118      foreach ($node->taxonomy as $term) {
119        $original_term = $term;
120        if ((object)$term) {
121          // With free-tagging it's somewhat hard to get the tid, vid, name values
122          // Rather than duplicating taxonomy.module code here you should make sure your calling module
123          // has a weight of at least 1 which will run after taxonomy has saved the data which allows us to
124          // pull it out of the db here.
125          if (!isset($term->name) || !isset($term->tid)) {
126            $vid = db_result(db_query_range("SELECT t.vid FROM {term_node} r INNER JOIN {term_data} t ON r.tid = t.tid INNER JOIN {vocabulary} v ON t.vid = v.vid WHERE r.nid = %d ORDER BY v.weight, t.weight, t.name", $object->nid, 0, 1));
127            if (!$vid) {
128              continue;
129            }
130            $term = db_fetch_object(db_query_range("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.vid = %d ORDER BY t.weight", $vid, $object->vid, 0, 1));
131            $term->vid = $vid;
132          }
133
134          // Ok, if we still don't have a term name maybe this is a pre-taxonomy submit node
135          // So if it's a number we can get data from it
136          if (!isset($term->name) && is_array($original_term)) {
137            $tid = array_shift($original_term);
138            if (is_numeric($tid)) {
139              $term = taxonomy_get_term($tid);
140            }
141          }
142          $values['term'] = check_plain($term->name);
143          $values['term-raw'] = $term->name;
144          $values['term-id'] = $term->tid;
145          $vid = $term->vid;
146
147          if (!empty($vid)) {
148            $vocabulary = taxonomy_vocabulary_load($vid);
149            $values['vocab'] = check_plain($vocabulary->name);
150            $values['vocab-raw'] = $vocabulary->name;
151            $values['vocab-id'] = $vocabulary->vid;
152          }
153
154          // The 'catpath' (and 'cat') tokens have been removed, as they caused quite a bit of confusion,
155          // and the catpath was a relatively expensive query when the taxonomy tree was deep.
156          //
157          // It existed only to provide forward-compatability with pathauto module, and
158          // for most uses of token.module, it was a relatively useless token -- it exposed
159          // a list of term names formatted as a URL/path string. Once pathauto supports
160          // tokens, *it* should handle this catpath alias as it's the primary consumer.
161          break;
162        }
163      }
164    }
165    // It's possible to leave that block and still not have good data.
166    // So, we test for these and if not set, set them.
167    if (!isset($values['term'])) {
168      $values['term'] = '';
169      $values['term-raw'] = '';
170      $values['term-id'] = '';
171      $values['vocab'] = '';
172      $values['vocab-raw'] = '';
173      $values['vocab-id'] = '';
174    }
175  }
176
177  return $values;
178}
179
180/**
181 * Implements hook_token_list() on behalf of menu.module.
182 */
183function menu_token_list($type = 'all') {
184  $tokens = array();
185
186  if ($type == 'node' || $type == 'all') {
187    $tokens['node']['menu']                = t("The name of the menu the node belongs to.");
188    $tokens['node']['menu-raw']            = t("The name of the menu the node belongs to.");
189    $tokens['node']['menupath']            = t("The menu path (as reflected in the breadcrumb), not including Home or [menu]. Separated by /.");
190    $tokens['node']['menupath-raw']        = t("The unfiltered menu path (as reflected in the breadcrumb), not including Home or [menu]. Separated by /.");
191    $tokens['node']['menu-link-title']     = t("The text used in the menu as link text for this item.");
192    $tokens['node']['menu-link-title-raw'] = t("The unfiltered text used in the menu as link text for this item.");
193    $tokens['node']['menu-link-mlid']      = t("The unique ID of the node's menu link.");
194    $tokens['node']['menu-link-plid']      = t("The unique ID of the node's menu link parent.");
195    $tokens['node']['menu-link-parent-path']     = t('The URL alias of the parent menu link of the node.');
196    $tokens['node']['menu-link-parent-path-raw'] = t('The URL alias of the parent menu link of the node.');
197  }
198
199  return $tokens;
200}
201
202/**
203 * Implements hook_token_values() on behalf of menu.module.
204 */
205function menu_token_values($type, $object = NULL, $options = array()) {
206  static $menus;
207
208  $values = array();
209
210  // Statically cache menu_get_menus() since this causes a database query
211  // every time it is called, and is not likely to change in the same page
212  // request.
213  if (!isset($menus)) {
214    $menus = menu_get_menus();
215  }
216
217  if ($type == 'node' && !empty($object)) {
218    $node = $object;
219
220    // Initialize tokens to empty strings.
221    $values['menu']                = '';
222    $values['menu-raw']            = '';
223    $values['menupath']            = '';
224    $values['menupath-raw']        = '';
225    $values['menu-link-title']     = '';
226    $values['menu-link-title-raw'] = '';
227    $values['menu-link-mlid']      = '';
228    $values['menu-link-plid']      = '';
229    $values['menu-link-parent-path'] = '';
230    $values['menu-link-parent-path-raw'] = '';
231
232    if (!isset($node->menu)) {
233      // We need to clone the node as menu_nodeapi($node, 'prepare') may cause data loss.
234      // @see http://drupal.org/node/1317926
235      $node = drupal_clone($node);
236      // Nodes do not have their menu links loaded via menu_nodeapi($node, 'load').
237      menu_nodeapi($node, 'prepare');
238    }
239
240    // Now get the menu related information.
241    if (!empty($node->menu['mlid'])) {
242      $link = token_menu_link_load($node->menu['mlid']);
243
244      if (isset($menus[$link['menu_name']])) {
245        $values['menu-raw'] = $menus[$link['menu_name']];
246        $values['menu']     = check_plain($values['menu-raw']);
247      }
248
249      $parents = token_menu_link_get_parents_all($link['mlid']);
250      $trail_raw = array();
251      foreach ($parents as $parent) {
252        $trail_raw[] = $parent['title'];
253      }
254      $trail = array_map('check_plain', $trail_raw);
255
256      $values['menupath']            = !empty($options['pathauto']) ? $trail : implode('/', $trail);
257      $values['menupath-raw']        = !empty($options['pathauto']) ? $trail_raw : implode('/', $trail_raw);
258      $values['menu-link-title']     = check_plain($link['title']);
259      $values['menu-link-title-raw'] = $link['title'];
260      $values['menu-link-mlid']      = $link['mlid'];
261
262      if (!empty($link['plid']) && $parent = token_menu_link_load($link['plid'])) {
263        $values['menu-link-plid'] = $parent['mlid'];
264        $values['menu-link-parent-path-raw'] = drupal_get_path_alias($parent['href']);
265        $values['menu-link-parent-path']     = check_plain($values['menu-link-parent-path-raw']);
266      }
267    }
268  }
269
270  return $values;
271}
272
273/**
274 * Implements hook_token_list() on behalf of book.module.
275 */
276function book_token_list($type) {
277  $tokens = array();
278
279  if ($type == 'node' || $type == 'all') {
280    $tokens['book']['book']         = t("The title of the node's book parent.");
281    $tokens['book']['book-raw']     = t("The title of the node's book parent.");
282    $tokens['book']['book_id']      = t("The id of the node's book parent.");
283    $tokens['book']['bookpath']     = t("The titles of all parents in the node's book hierarchy.");
284    $tokens['book']['bookpath-raw'] = t("The titles of all parents in the node's book hierarchy.");
285  }
286
287  return $tokens;
288}
289
290/**
291 * Implements hook_token_values() on behalf of book.module.
292 */
293function book_token_values($type, $object = NULL, $options = array()) {
294  $values = array();
295
296  if ($type == 'node' && !empty($object)) {
297    $node = $object;
298
299    // Initialize tokens to empty strings.
300    $values['book'] = '';
301    $values['book-raw'] = '';
302    $values['book_id'] = '';
303    $values['bookpath'] = '';
304    $values['bookpath-raw'] = '';
305
306    if (!empty($node->book['mlid'])) {
307      // Exclude the current node's title from the book path trail (start with
308      // the book link's plid rather than mlid).
309      $parents = token_menu_link_get_parents_all($node->book['plid']);
310      $trail_raw = array();
311      foreach ($parents as $parent) {
312        $trail_raw[] = $parent['title'];
313      }
314      $trail = array_map('check_plain', $trail_raw);
315
316      // Load the root book page.
317      $root = token_menu_link_load($node->book['p1']);
318
319      $values['book'] = check_plain($root['title']);
320      $values['book-raw'] = $root['title'];
321      $values['book_id'] = $node->book['bid'];
322      $values['bookpath'] = !empty($options['pathauto']) ? $trail : implode('/', $trail);
323      $values['bookpath-raw'] = !empty($options['pathauto']) ? $trail_raw : implode('/', $trail_raw);
324    }
325  }
326
327  return $values;
328}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.