views.tokens.inc 4.13 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Token integration for the views module.
 */

8
use Drupal\Core\Render\BubbleableMetadata;
9

merlinofchaos's avatar
merlinofchaos committed
10 11 12 13
/**
 * Implements hook_token_info().
 */
function views_token_info() {
14
  $info['types']['view'] = [
15
    'name' => t('View', [], ['context' => 'View entity type']),
merlinofchaos's avatar
merlinofchaos committed
16 17
    'description' => t('Tokens related to views.'),
    'needs-data' => 'view',
18 19
  ];
  $info['tokens']['view']['label'] = [
20 21
    'name' => t('Label'),
    'description' => t('The label of the view.'),
22 23
  ];
  $info['tokens']['view']['description'] = [
merlinofchaos's avatar
merlinofchaos committed
24 25
    'name' => t('Description'),
    'description' => t('The description of the view.'),
26 27
  ];
  $info['tokens']['view']['id'] = [
28 29
    'name' => t('ID'),
    'description' => t('The machine-readable ID of the view.'),
30 31
  ];
  $info['tokens']['view']['title'] = [
merlinofchaos's avatar
merlinofchaos committed
32 33
    'name' => t('Title'),
    'description' => t('The title of current display of the view.'),
34 35
  ];
  $info['tokens']['view']['url'] = [
merlinofchaos's avatar
merlinofchaos committed
36 37 38
    'name' => t('URL'),
    'description' => t('The URL of the view.'),
    'type' => 'url',
39 40
  ];
  $info['tokens']['view']['base-table'] = [
41 42
    'name' => t('Base table'),
    'description' => t('The base table used for this view.'),
43 44
  ];
  $info['tokens']['view']['base-field'] = [
45 46
    'name' => t('Base field'),
    'description' => t('The base field used for this view.'),
47 48
  ];
  $info['tokens']['view']['total-rows'] = [
49 50
    'name' => t('Total rows'),
    'description' => t('The total amount of results returned from the view. The current display will be used.'),
51 52
  ];
  $info['tokens']['view']['items-per-page'] = [
53 54
    'name' => t('Items per page'),
    'description' => t('The number of items per page.'),
55 56
  ];
  $info['tokens']['view']['current-page'] = [
57 58
    'name' => t('Current page'),
    'description' => t('The current page of results the view is on.'),
59 60
  ];
  $info['tokens']['view']['page-count'] = [
61 62
    'name' => t('Page count'),
    'description' => t('The total page count.'),
63
  ];
merlinofchaos's avatar
merlinofchaos committed
64 65 66 67 68 69 70

  return $info;
}

/**
 * Implements hook_tokens().
 */
71
function views_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
72
  $url_options = ['absolute' => TRUE];
merlinofchaos's avatar
merlinofchaos committed
73 74 75
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
  }
76
  $replacements = [];
merlinofchaos's avatar
merlinofchaos committed
77 78

  if ($type == 'view' && !empty($data['view'])) {
79
    /** @var \Drupal\views\ViewExecutable $view */
merlinofchaos's avatar
merlinofchaos committed
80 81
    $view = $data['view'];

82 83
    $bubbleable_metadata->addCacheableDependency($view->storage);

merlinofchaos's avatar
merlinofchaos committed
84 85
    foreach ($tokens as $name => $original) {
      switch ($name) {
86
        case 'label':
87
          $replacements[$original] = $view->storage->label();
merlinofchaos's avatar
merlinofchaos committed
88 89 90
          break;

        case 'description':
91
          $replacements[$original] = $view->storage->get('description');
merlinofchaos's avatar
merlinofchaos committed
92 93
          break;

94 95
        case 'id':
          $replacements[$original] = $view->storage->id();
merlinofchaos's avatar
merlinofchaos committed
96 97 98
          break;

        case 'title':
99
          $title = $view->getTitle();
100
          $replacements[$original] = $title;
merlinofchaos's avatar
merlinofchaos committed
101 102 103
          break;

        case 'url':
104 105 106 107 108 109 110 111 112
          try {
            if ($url = $view->getUrl()) {
              $replacements[$original] = $url->setOptions($url_options)
                ->toString();
            }
          }
          catch (\InvalidArgumentException $e) {
            // The view has no URL so we leave the value empty.
            $replacements[$original] = '';
merlinofchaos's avatar
merlinofchaos committed
113 114
          }
          break;
115
        case 'base-table':
116
          $replacements[$original] = $view->storage->get('base_table');
117 118
          break;
        case 'base-field':
119
          $replacements[$original] = $view->storage->get('base_field');
120 121
          break;
        case 'total-rows':
122
          $replacements[$original] = (int) $view->total_rows;
123 124 125 126 127 128 129 130 131 132
          break;
        case 'items-per-page':
          $replacements[$original] = (int) $view->getItemsPerPage();
          break;
        case 'current-page':
          $replacements[$original] = (int) $view->getCurrentPage() + 1;
          break;
        case 'page-count':
          // If there are no items per page, set this to 1 for the division.
          $per_page = $view->getItemsPerPage() ?: 1;
133
          $replacements[$original] = max(1, (int) ceil($view->total_rows / $per_page));
134
          break;
merlinofchaos's avatar
merlinofchaos committed
135 136 137 138 139 140
      }
    }
  }

  return $replacements;
}