function ViewsTokensHooks::tokens
Implements hook_tokens().
File
-
core/
modules/ views/ src/ Hook/ ViewsTokensHooks.php, line 60
Class
- ViewsTokensHooks
- Hook implementations for views.
Namespace
Drupal\views\HookCode
public function 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) {
// 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.