views.tokens.inc 4.21 KB
Newer Older
1 2 3 4 5 6 7
<?php

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

8
use Drupal\Component\Utility\Html;
9
use Drupal\Core\Render\BubbleableMetadata;
10

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

  return $info;
}

/**
 * Implements hook_tokens().
 */
72
function views_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
73 74 75 76 77 78 79 80 81
  $url_options = array('absolute' => TRUE);
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
  }
  $sanitize = !empty($options['sanitize']);

  $replacements = array();

  if ($type == 'view' && !empty($data['view'])) {
82
    /** @var \Drupal\views\ViewExecutable $view */
83 84
    $view = $data['view'];

85 86
    $bubbleable_metadata->addCacheableDependency($view->storage);

87 88
    foreach ($tokens as $name => $original) {
      switch ($name) {
89
        case 'label':
90
          $replacements[$original] = $sanitize ? Html::escape($view->storage->label()) : $view->storage->label();
91 92 93
          break;

        case 'description':
94
          $replacements[$original] = $sanitize ? Html::escape($view->storage->get('description')) : $view->storage->get('description');
95 96
          break;

97 98
        case 'id':
          $replacements[$original] = $view->storage->id();
99 100 101
          break;

        case 'title':
102
          $title = $view->getTitle();
103
          $replacements[$original] = $sanitize ? Html::escape($title) : $title;
104 105 106
          break;

        case 'url':
107
          if ($url = $view->getUrl()) {
108 109
            $replacements[$original] = $url->setOptions($url_options)
              ->toString();
110 111
          }
          break;
112
        case 'base-table':
113
          $replacements[$original] = $view->storage->get('base_table');
114 115
          break;
        case 'base-field':
116
          $replacements[$original] = $view->storage->get('base_field');
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
          break;
        case 'total-rows':
          $replacements[$original] = count($view->result);
          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;
          $replacements[$original] = (int) ceil(count($view->result) / $per_page);
          break;
132 133 134 135 136 137
      }
    }
  }

  return $replacements;
}