Project

General

Profile

Paste
Download (2.48 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / views.tokens.inc @ 4003efde

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

    
52
  $replacements = array();
53

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

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

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

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

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

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

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

    
92
  return $replacements;
93
}