I have a view with multiple pages. In the Views interface, I can set a display name for each display.
Is there anyway I can print this name in the view itself, for example adding a Global Text field to the views header?
In Drupal 7, the "Global: Result summary" field in Views header gives you a few tokens to get access to some of the view information (e.g. it has a token named @name -- the human-readable name of the view
to print the View name).
For fetching the View display name, though, you need to create a custom Views handler to define your own tokens:
my_module_name.info
file, add the lines : dependencies[] = views
files[] = handlers/views_handler_my_custom_handler.inc
my_module.module
file, add a hook_views_data(): function my_module_views_data() {
// Definition of your custom handler
$data['views']['my_module_custom_handler'] = array(
'title' => t('My custom handler'),
'help' => t('Custom tokens to access View information'),
'area' => array(
'handler' => 'views_handler_my_custom_handler',
),
);
return $data;
}
handlers/views_handler_my_custom_handler.inc
file. The most basic content based on your needs would be something like:
/**
* @file
* Definition of views_handler_area.
*/
/**
* Views area handler to display some configurable result summary.
*
* @ingroup views_area_handlers
*/
class views_handler_my_custom_handler extends views_handler_area {
function option_definition() {
$options = parent::option_definition();
$options['content'] = array(
'default' => 'Displaying view tokens',
'translatable' => TRUE,
);
return $options;
}
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$variables = array(
'items' => array(
'@display_name -- the name of the View display'
),
);
$list = theme('item_list', $variables);
$form['content'] = array(
'#title' => t('Display'),
'#type' => 'textarea',
'#rows' => 3,
'#default_value' => $this->options['content'],
'#description' => t('You may use HTML code in this field. The following tokens are supported:') . $list,
);
}
/**
* Find out the information to render.
*/
function render($empty = FALSE) {
$format = $this->options['content'];
// Replace "@display_name" with view current display value
$output = filter_xss_admin(str_replace("@display_name", $this->view->current_display, $format));
return $output;
}
That's it. Your custom views handler should now be available in any View.
I recommend you to have a look at view_handler_area_result.inc file (code for "Global: Result summary" field) in Views module if you wish to extend it to have its tokens available in your custom handler.
@name
prints Indexes
rather than the display name of the page.
Commented
Sep 16, 2021 at 10:46