views.tokens.inc

Token integration for the views module.

File

drupal/core/modules/views/views.tokens.inc
View source
<?php

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

/**
 * Implements hook_token_info().
 */
function views_token_info() {
  $info['types']['view'] = array(
    'name' => t('View'),
    'description' => t('Tokens related to views.'),
    'needs-data' => 'view',
  );
  $info['tokens']['view']['name'] = array(
    'name' => t('Name'),
    'description' => t('The human-readable name of the view.'),
  );
  $info['tokens']['view']['description'] = array(
    'name' => t('Description'),
    'description' => t('The description of the view.'),
  );
  $info['tokens']['view']['machine-name'] = array(
    'name' => t('Machine name'),
    'description' => t('The machine-readable name of the view.'),
  );
  $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',
  );
  $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.'),
  );
  return $info;
}

/**
 * Implements hook_tokens().
 */
function views_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $url_options = array(
    'absolute' => TRUE,
  );
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
  }
  $sanitize = !empty($options['sanitize']);
  $langcode = isset($options['language']) ? $options['language']->langcode : NULL;
  $replacements = array();
  if ($type == 'view' && !empty($data['view'])) {
    $view = $data['view'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'name':
          $replacements[$original] = $sanitize ? check_plain($view->storage
            ->getHumanName()) : $view->storage
            ->getHumanName();
          break;
        case 'description':
          $replacements[$original] = $sanitize ? check_plain($view->storage
            ->get('description')) : $view->storage
            ->get('description');
          break;
        case 'machine-name':
          $replacements[$original] = $view->storage
            ->get('name');
          break;
        case 'title':
          $title = $view
            ->getTitle();
          $replacements[$original] = $sanitize ? check_plain($title) : $title;
          break;
        case 'url':
          if ($path = $view
            ->getUrl()) {
            $replacements[$original] = url($path, $url_options);
          }
          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;
      }
    }

    // [view:url:*] nested tokens. This only works if Token module is installed.
    if ($url_tokens = token_find_with_prefix($tokens, 'url')) {
      if ($path = $view
        ->getUrl()) {
        $replacements += token_generate('url', $url_tokens, array(
          'path' => $path,
        ), $options);
      }
    }
  }
  return $replacements;
}

Functions

Namesort descending Description
views_tokens Implements hook_tokens().
views_token_info Implements hook_token_info().