2

In some of the views, I needed to customize the title using twig. For example {{ title }} which would read the title of the first row in the view result and place it in the page title. This works fine.

The problem is with the breadcrumb. It shows as Some Parent page > {{ title }} without replacing the {{ title }} with its actual value.

I guess a solution could be something along the lines of placing some code in THEME_preprocess_breadcrumb, but I am not sure what to write there exactly. But shouldn't this functionality be something out of the box?

P.S. Not all my views are using {{ title }}, actually some of them are a little more complex, and some views are parents of other views path-wise. So solutions like in this Q doesn't help.

1

I had a similar issue where I needed my view title to be {{ field_first_name }} {{ field_last_name }}. The head title tag and page title were correct, but the twig variable's value wasn't being rendered for the breadcrumb.

I ended up writing a tiny module in which I get the title then set the title to the title. Somehow, doing this in post_render makes the twig get rendered correctly in the breadcrumb. Hopefully someone will come along and tell us why this works or why it was needed.

?php
use Drupal\views\ViewExecutable;

/**
 * Implements hook_views_post_render().
 */
function peopleview_views_post_render(ViewExecutable $view) {
 if ($view->id() == 'people') {
  $title = $view->getTitle();
  // Set the view title.
  $view->setTitle($title);
  // Set the route title.
  $route = \Drupal::routeMatch()->getCurrentRouteMatch()->getRouteObject();
  $route->setDefault('_title', $title);
 }
}
2
  • 1
    Doesn't make sense to me that those folks don't do that already in core! – Bishoy Jan 2 '18 at 22:58
  • I have a view that is matching a node and a contextual filter and using two values from the referenced node field in the title. Page title was fine but the meta one was showing something like "bla {{ bla }} bla bla {{ bla }}" I used that function to solve it. Why isn't this issue solved yet? Or was I doing it wrong initially. – Jed_BH Sep 1 '18 at 12:09

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.