function views_tokens

Same name in other branches
  1. 9 core/modules/views/views.tokens.inc \views_tokens()
  2. 8.9.x core/modules/views/views.tokens.inc \views_tokens()
  3. 11.x core/modules/views/views.tokens.inc \views_tokens()

Implements hook_tokens().

File

core/modules/views/views.tokens.inc, line 71

Code

function views_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
    $url_options = [
        'absolute' => TRUE,
    ];
    if (isset($options['language'])) {
        $url_options['language'] = $options['language'];
    }
    $replacements = [];
    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':
                    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] = '';
                    }
                    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] = (int) $view->total_rows;
                    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] = max(1, (int) ceil($view->total_rows / $per_page));
                    break;
            }
        }
    }
    return $replacements;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.