Projet

Général

Profil

Paste
Télécharger (2,57 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / views / views.tokens.inc @ a5572547

1
<?php
2

    
3
/**
4
 * @file
5
 * Token integration for the views module.
6
 */
7

    
8
/**
9
 * Implements hook_token_info().
10
 */
11
function views_token_info() {
12
  $info['types']['view'] = array(
13
    'name' => t('View'),
14
    'description' => t('Tokens related to views.'),
15
    'needs-data' => 'view',
16
  );
17
  $info['tokens']['view']['name'] = array(
18
    'name' => t('Name'),
19
    'description' => t('The human-readable name of the view.'),
20
  );
21
  $info['tokens']['view']['description'] = array(
22
    'name' => t('Description'),
23
    'description' => t('The description of the view.'),
24
  );
25
  $info['tokens']['view']['machine-name'] = array(
26
    'name' => t('Machine name'),
27
    'description' => t('The machine-readable name of the view.'),
28
  );
29
  $info['tokens']['view']['title'] = array(
30
    'name' => t('Title'),
31
    'description' => t('The title of current display of the view.'),
32
  );
33
  $info['tokens']['view']['url'] = array(
34
    'name' => t('URL'),
35
    'description' => t('The URL of the view.'),
36
    'type' => 'url',
37
  );
38

    
39
  return $info;
40
}
41

    
42
/**
43
 * Implements hook_tokens().
44
 */
45
function views_tokens($type, $tokens, array $data = array(), array $options = array()) {
46
  $url_options = array('absolute' => TRUE);
47
  if (isset($options['language'])) {
48
    $url_options['language'] = $options['language'];
49
  }
50
  $sanitize = !empty($options['sanitize']);
51
  $langcode = isset($options['language']) ? $options['language']->language : NULL;
52

    
53
  $replacements = array();
54

    
55
  if ($type == 'view' && !empty($data['view'])) {
56
    $view = $data['view'];
57

    
58
    foreach ($tokens as $name => $original) {
59
      switch ($name) {
60
        case 'name':
61
          $replacements[$original] = $sanitize ? check_plain($view->human_name) : $view->human_name;
62
          break;
63

    
64
        case 'description':
65
          $replacements[$original] = $sanitize ? check_plain($view->description) : $view->description;
66
          break;
67

    
68
        case 'machine-name':
69
          $replacements[$original] = $view->name;
70
          break;
71

    
72
        case 'title':
73
          $title = $view->get_title();
74
          $replacements[$original] = $sanitize ? check_plain($title) : $title;
75
          break;
76

    
77
        case 'url':
78
          if ($path = $view->get_url()) {
79
            $replacements[$original] = url($path, $url_options);
80
          }
81
          break;
82
      }
83
    }
84

    
85
    // [view:url:*] nested tokens. This only works if Token module is installed.
86
    if ($url_tokens = token_find_with_prefix($tokens, 'url')) {
87
      if ($path = $view->get_url()) {
88
        $replacements += token_generate('url', $url_tokens, array('path' => $path), $options);
89
      }
90
    }
91
  }
92

    
93
  return $replacements;
94
}