You are here

function views_tokens in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/views.tokens.inc \views_tokens()

Implements hook_tokens().

File

core/modules/views/views.tokens.inc, line 71
Token integration for the views module.

Code

function views_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $url_options = array(
    'absolute' => TRUE,
  );
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
  }
  $replacements = array();
  if ($type == 'view' && !empty($data['view'])) {

    /** @var \Drupal\views\ViewExecutable $view */
    $view = $data['view'];
    $bubbleable_metadata
      ->addCacheableDependency($view->storage);
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'label':
          $replacements[$original] = $view->storage
            ->label();
          break;
        case 'description':
          $replacements[$original] = $view->storage
            ->get('description');
          break;
        case 'id':
          $replacements[$original] = $view->storage
            ->id();
          break;
        case 'title':
          $title = $view
            ->getTitle();
          $replacements[$original] = $title;
          break;
        case 'url':
          if ($url = $view
            ->getUrl()) {
            $replacements[$original] = $url
              ->setOptions($url_options)
              ->toString();
          }
          break;
        case 'base-table':
          $replacements[$original] = $view->storage
            ->get('base_table');
          break;
        case 'base-field':
          $replacements[$original] = $view->storage
            ->get('base_field');
          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;
      }
    }
  }
  return $replacements;
}