0

I have a view with multiple pages. In the Views interface, I can set a display name for each display.

screenshot

Is there anyway I can print this name in the view itself, for example adding a Global Text field to the views header?

0

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:

  • Create a new module. In your my_module_name.info file, add the lines :
    dependencies[] = views

    files[] = handlers/views_handler_my_custom_handler.inc
  • In your 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;
    }
  • Finally, create the 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.

2
  • Thanks! That seems to print the name of the View, rather than the display name of the page. E.g. A view is called "Indexes". And in that view, you create multiple pages, each which have a different display name. @name prints Indexes rather than the display name of the page.
    – big_smile
    Sep 16 at 10:46
  • My bad, you are absolutely right! Basic Views tokens are not enough for what you need. Please see my edited answer.
    – misterdidi
    Sep 20 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.