Similar to Views: how to programmatically change Views title?, I want to update the title of a Views page using logic/data only possible in PHP.
However, I want this to update only the appropriate
part of the <title>
of the HTML <head>
without mirroring the change in the displayed title in the <body>
's <h1 class="page-header">
.
To illustrate, imagine a view with some exposed filters that are used for navigating a product catalogue. Currently, every filtering of this page has the same titles:
<title>Products | My site name</title>
...
<h1 class="page-header">Products</h1>
I've written some code requiring access to $view->exposed_input
that generates an appropriate string based on exposed filter choices (for example, "red, low cost, good condition"), similar to Using exposed filters as part of the rendered page's title. I want these in the html head title, so each navigable URL has a unique title with appropriate keywords for the content. I don't want them in the <h1>
, however, because they're already clearly visible in the exposed filter page UI and I don't want to duplicate the information. I want it to look like this:
<title>Products: red, low cost, good condition | My site name</title>
...
<h1 class="page-header">Products</h1>
Things I've tried:
Everything on that linked question applies to both the
head
andbody
titles<title>Products: red, low cost, good condition | My site name</title> ... <h1 class="page-header">Products: red, low cost, good condition</h1>
drupal_set_title()
doesn't work at all from any of the Views hooks I've tried, presumably because the view overrides this further down the line. I've tried:hook_preprocess_views_view
hook_views_pre_render
hook_views_post_render
Setting
$vars['head_title']
inhook_preprocess_views_view
also doesn't work, presumably for the same reason: Views overrides this.- Someone commenting on the hook_views_post_render API page with the exact same problem as me 3 years ago reports that it still doesn't work if you pass the variable to through to
hook_preprocess_html
and apply it to$vars['head_title']
from there
- Someone commenting on the hook_views_post_render API page with the exact same problem as me 3 years ago reports that it still doesn't work if you pass the variable to through to