3

In D8, I have a need to dynamically set the title of a views page display.

I've tried implementing both hook_views_pre_render and hook_views_post_build and setting the value of $view->build_info['title'], but Drupal seems to be ignoring it when setting the page title.

It seems like the code in Views where the title is set is in Drupal\views\Plugin\views\display\Page::execute()

The #title property on the render array in that method IS receiving my overwritten value from one of my hooks, but it's still ignore and is using the manually specified title on the display config.

What is going on!?

4
  • 1
    Do you have the metatag module enabled? It is still missing Views integration (drupal.org/node/2563647) so perhaps it overrides your title with the global default? Feb 28 '17 at 19:37
  • 1
    I do, but if that were the case, then specific views title I specify in the views config would not work either, but it does.
    – Brian
    Feb 28 '17 at 19:39
  • 1
    Is this view using a custom template file?
    – No Sssweat
    Feb 28 '17 at 23:34
  • 2
    What is going on!? The title is set in the route definition. See drupal.stackexchange.com/questions/224217/…
    – 4k4
    Mar 1 '17 at 8:59
6

If someone else is looking for an answer, Here is how I achieved this. I implemented hook_views_pre_render and in this hook, I implemented a method of views class setTitle. This allowed me to set the page title.

  1. At the beginning of your MYMODULE.module

     use Drupal\views\ViewExecutable
    
  2. The complete code.

    function MYMODULE_views_pre_render(ViewExecutable $view) {

    function MYMODULE_views_pre_render(ViewExecutable $view) {
      if ($view->id() == 'ABC') {
        $view->setTitle('MY CUSTOM TITLE'); 
      }
    }
    

Hope this helps.

1
  • Just to appreciate you that this answer worked.
    – Pupil
    Dec 27 '17 at 12:48
2

If you want to change page title too you can use this snippet

\Drupal::routeMatch()->getRouteObject()->setDefault('_title', $title);

Hope this helps.

1
  • This is not working for me. Aug 2 '19 at 6:08
0

Setting only the _title property to set the page title tag was not enough in my case.

Overriding the title_callback property did the trick :-)

$route->setDefault('_title_callback', function() use ($title) {
  return $title;
});
0

Implemented using hook_views_post_render(). In this example the contextual filter is just added to the title.

/**
 * Implements hook_views_post_render().
 */
function THEME_views_post_render(&$view) {
    /** @var \Drupal\views\ViewExecutable $view */
    if ($view->id() === 'events' && isset($view->args[0])) {
        // Get year from contextual argument of the view.
        $year = $view->args[0];

        // Prefix year to title.
        $title = (string) t('@year Events', ['@year' => $year]);

        // Set view and page titles.
        $view->setTitle($title);
        \Drupal::routeMatch()->getRouteObject()->setDefault(
            '_title',
            $title
        );
    }
}

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.