Score:0

How do I print the display name?

us flag

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?

Score:0
de flag

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.

big_smile avatar
us flag
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.
misterdidi avatar
de flag
My bad, you are absolutely right! Basic Views tokens are not enough for what you need. Please see my edited answer.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.