Projet

Général

Profil

Paste
Télécharger (6,59 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / node / node.tokens.inc @ 6eb57d7f

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
            $instance = field_info_instance('node', 'body', $node->type);
140
            $field_langcode = field_language('node', $node, 'body', $language_code);
141
            // If the summary was requested and is not empty, use it.
142
            if ($name == 'summary' && !empty($items[0]['summary'])) {
143
              $output = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'summary') : $items[0]['summary'];
144
            }
145
            // Attempt to provide a suitable version of the 'body' field.
146
            else {
147
              $output = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'value') : $items[0]['value'];
148
              // A summary was requested.
149
              if ($name == 'summary') {
150
                if (isset($instance['display']['teaser']['settings']['trim_length'])) {
151
                  $trim_length = $instance['display']['teaser']['settings']['trim_length'];
152
                }
153
                else {
154
                  // Use default value.
155
                  $trim_length = NULL;
156
                }
157
                // Generate an optionally trimmed summary of the body field.
158
                $output = text_summary($output, $instance['settings']['text_processing'] ? $items[0]['format'] : NULL, $trim_length);
159
              }
160
            }
161
            $replacements[$original] = $output;
162
          }
163
          break;
164

    
165
        case 'language':
166
          $langcode = entity_language('node', $node);
167
          $replacements[$original] = $sanitize ? check_plain($langcode) : $langcode;
168
          break;
169

    
170
        case 'url':
171
          $replacements[$original] = url('node/' . $node->nid, $url_options);
172
          break;
173

    
174
        case 'edit-url':
175
          $replacements[$original] = url('node/' . $node->nid . '/edit', $url_options);
176
          break;
177

    
178
        // Default values for the chained tokens handled below.
179
        case 'author':
180
          $account = user_load($node->uid);
181
          $name = format_username($account);
182
          $replacements[$original] = $sanitize ? check_plain($name) : $name;
183
          break;
184

    
185
        case 'created':
186
          $replacements[$original] = format_date($node->created, 'medium', '', NULL, $language_code);
187
          break;
188

    
189
        case 'changed':
190
          $replacements[$original] = format_date($node->changed, 'medium', '', NULL, $language_code);
191
          break;
192
      }
193
    }
194

    
195
    if ($author_tokens = token_find_with_prefix($tokens, 'author')) {
196
      $author = user_load($node->uid);
197
      $replacements += token_generate('user', $author_tokens, array('user' => $author), $options);
198
    }
199

    
200
    if ($created_tokens = token_find_with_prefix($tokens, 'created')) {
201
      $replacements += token_generate('date', $created_tokens, array('date' => $node->created), $options);
202
    }
203

    
204
    if ($changed_tokens = token_find_with_prefix($tokens, 'changed')) {
205
      $replacements += token_generate('date', $changed_tokens, array('date' => $node->changed), $options);
206
    }
207
  }
208

    
209
  return $replacements;
210
}