Projet

Général

Profil

Paste
Télécharger (5,63 ko) Statistiques
| Branche: | Révision:

root / htmltest / modules / node / node.tokens.inc @ 85ad3d82

1
<?php
2

    
3
/**
4
 * @file
5
 * Builds placeholder replacement tokens for node-related data.
6
 */
7

    
8

    
9

    
10
/**
11
 * Implements hook_token_info().
12
 */
13
function node_token_info() {
14
  $type = array(
15
    'name' => t('Nodes'),
16
    'description' => t('Tokens related to individual content items, or "nodes".'),
17
    'needs-data' => 'node',
18
  );
19

    
20
  // Core tokens for nodes.
21
  $node['nid'] = array(
22
    'name' => t("Content ID"),
23
    'description' => t('The unique ID of the content item, or "node".'),
24
  );
25
  $node['vid'] = array(
26
    'name' => t("Revision ID"),
27
    'description' => t("The unique ID of the node's latest revision."),
28
  );
29
  $node['tnid'] = array(
30
    'name' => t("Translation set ID"),
31
    'description' => t("The unique ID of the original-language version of this node, if one exists."),
32
  );
33
  $node['type'] = array(
34
    'name' => t("Content type"),
35
    'description' => t("The type of the node."),
36
  );
37
  $node['type-name'] = array(
38
    'name' => t("Content type name"),
39
    'description' => t("The human-readable name of the node type."),
40
  );
41
  $node['title'] = array(
42
    'name' => t("Title"),
43
    'description' => t("The title of the node."),
44
  );
45
  $node['body'] = array(
46
    'name' => t("Body"),
47
    'description' => t("The main body text of the node."),
48
  );
49
  $node['summary'] = array(
50
    'name' => t("Summary"),
51
    'description' => t("The summary of the node's main body text."),
52
  );
53
  $node['language'] = array(
54
    'name' => t("Language"),
55
    'description' => t("The language the node is written in."),
56
  );
57
  $node['url'] = array(
58
    'name' => t("URL"),
59
    'description' => t("The URL of the node."),
60
  );
61
  $node['edit-url'] = array(
62
    'name' => t("Edit URL"),
63
    'description' => t("The URL of the node's edit page."),
64
  );
65

    
66
  // Chained tokens for nodes.
67
  $node['created'] = array(
68
    'name' => t("Date created"),
69
    'description' => t("The date the node was posted."),
70
    'type' => 'date',
71
  );
72
  $node['changed'] = array(
73
    'name' => t("Date changed"),
74
    'description' => t("The date the node was most recently updated."),
75
    'type' => 'date',
76
  );
77
  $node['author'] = array(
78
    'name' => t("Author"),
79
    'description' => t("The author of the node."),
80
    'type' => 'user',
81
  );
82

    
83
  return array(
84
    'types' => array('node' => $type),
85
    'tokens' => array('node' => $node),
86
  );
87
}
88

    
89
/**
90
 * Implements hook_tokens().
91
 */
92
function node_tokens($type, $tokens, array $data = array(), array $options = array()) {
93
  $url_options = array('absolute' => TRUE);
94
  if (isset($options['language'])) {
95
    $url_options['language'] = $options['language'];
96
    $language_code = $options['language']->language;
97
  }
98
  else {
99
    $language_code = NULL;
100
  }
101
  $sanitize = !empty($options['sanitize']);
102

    
103
  $replacements = array();
104

    
105
  if ($type == 'node' && !empty($data['node'])) {
106
    $node = $data['node'];
107

    
108
    foreach ($tokens as $name => $original) {
109
      switch ($name) {
110
        // Simple key values on the node.
111
        case 'nid':
112
          $replacements[$original] = $node->nid;
113
          break;
114

    
115
        case 'vid':
116
          $replacements[$original] = $node->vid;
117
          break;
118

    
119
        case 'tnid':
120
          $replacements[$original] = $node->tnid;
121
          break;
122

    
123
        case 'type':
124
          $replacements[$original] = $sanitize ? check_plain($node->type) : $node->type;
125
          break;
126

    
127
        case 'type-name':
128
          $type_name = node_type_get_name($node);
129
          $replacements[$original] = $sanitize ? check_plain($type_name) : $type_name;
130
          break;
131

    
132
        case 'title':
133
          $replacements[$original] = $sanitize ? check_plain($node->title) : $node->title;
134
          break;
135

    
136
        case 'body':
137
        case 'summary':
138
          if ($items = field_get_items('node', $node, 'body', $language_code)) {
139
            $column = ($name == 'body') ? 'value' : 'summary';
140
            $instance = field_info_instance('node', 'body', $node->type);
141
            $field_langcode = field_language('node', $node, 'body', $language_code);
142
            $replacements[$original] = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], $column) : $items[0][$column];
143
          }
144
          break;
145

    
146
        case 'language':
147
          $langcode = entity_language('node', $node);
148
          $replacements[$original] = $sanitize ? check_plain($langcode) : $langcode;
149
          break;
150

    
151
        case 'url':
152
          $replacements[$original] = url('node/' . $node->nid, $url_options);
153
          break;
154

    
155
        case 'edit-url':
156
          $replacements[$original] = url('node/' . $node->nid . '/edit', $url_options);
157
          break;
158

    
159
        // Default values for the chained tokens handled below.
160
        case 'author':
161
          $account = user_load($node->uid);
162
          $name = format_username($account);
163
          $replacements[$original] = $sanitize ? check_plain($name) : $name;
164
          break;
165

    
166
        case 'created':
167
          $replacements[$original] = format_date($node->created, 'medium', '', NULL, $language_code);
168
          break;
169

    
170
        case 'changed':
171
          $replacements[$original] = format_date($node->changed, 'medium', '', NULL, $language_code);
172
          break;
173
      }
174
    }
175

    
176
    if ($author_tokens = token_find_with_prefix($tokens, 'author')) {
177
      $author = user_load($node->uid);
178
      $replacements += token_generate('user', $author_tokens, array('user' => $author), $options);
179
    }
180

    
181
    if ($created_tokens = token_find_with_prefix($tokens, 'created')) {
182
      $replacements += token_generate('date', $created_tokens, array('date' => $node->created), $options);
183
    }
184

    
185
    if ($changed_tokens = token_find_with_prefix($tokens, 'changed')) {
186
      $replacements += token_generate('date', $changed_tokens, array('date' => $node->changed), $options);
187
    }
188
  }
189

    
190
  return $replacements;
191
}