Problem
The tag <title>
is hardcoded into the html.html.twig tpl, and his value is populated with the page title in theme.inc hook_preprocess_html.
Adding a ['#attached']['html_head'][] = [$my_tag, 'title'];
to a render array do not override the page title value, but also add another title tag to the html head.
In case of a render array that need to override the <title>
, the normal way to do it, cause of the cache, will be to use #attached. But for now that works for any head tags but the <title>
In fact, now, the only way to override title is to implement a hook_preprocess_html and override $variables['head_title']...
Solution
To be discuted
1) Detect the #attached title, in theme.inc preprocess_html and make conditionnal the value of the head_title
2) Remove the head_title variables and make module that produce "pages" responsible of the generation of this [#attached][html_head][] [$tag, 'title']...
3) Please help me :))
How I found it
When I add this code in a hook_views_pre_render($view)
$tag = [
'#tag' => 'title',
'#value' => 'My awesome title',
];
$view->element['#attached']['html_head'][] = [$tag, 'title'];
I got two tags <title></title>
in the <head>
.
One with the views page title, and the one I added in my hook.. The one I added in the hook views pre render is in first position.
Comments
Comment #2
delta CreditAttribution: delta commentedComment #3
joelpittetI'm surprised you got it to make duplicate tags. But still good on you for discovering this.
Changing to the dev branch as that is the one we can test things against and hopefully if we have a solution we can make some regression tests.
Comment #4
delta CreditAttribution: delta commentedquestion answeredComment #5
delta CreditAttribution: delta commentedIn theme.inc preprocess_html :
If I change the line
$variables['head_title'] = $head_title;
toand i remove in the tpl the hardcoded
<title>{{ head_title|safe_join(' | ') }}</title>
I have only one title not the one I added in my hook_views_pre_render() but the one in the preprocess_html theme.inc.
So #attached [html_head] do not produce duplicate tag. It's working well. It's only because of the way the
<title>
tag is hardcoded.I just think the way it's done in D8, it's now impossible to change the page title with #attached ['html_head']
It's mandatory to implement a hook_preprocess_html and override the $variables['head_title'] :/
You can wish good luck on that to the metatag module :/
Comment #6
delta CreditAttribution: delta commentedComment #7
delta CreditAttribution: delta commentedComment #8
delta CreditAttribution: delta commentedComment #9
delta CreditAttribution: delta commentedComment #10
delta CreditAttribution: delta commentedComment #11
delta CreditAttribution: delta commentedBlocker for https://www.drupal.org/node/2563647
Comment #12
delta CreditAttribution: delta commentedComment #23
LendudeThis is how the metatag module handles this:
And then metatag_preprocess_html() does this:
Comment #25
marcoka CreditAttribution: marcoka commentedhook_preprocess_html will not always work. For example i have facets page where i tried to modify the . The title will only be changed if i edit that view. The actual page will have nothing set.
Altering the description for example works, doing that in the same way, will result in two tags.
Example test code
function facet_tests_seo_preprocess_html(&$variables) {
// Copy the "title" meta tag into the regular tag. The redundant meta
// tag is never added to $variables['#attached'].
// @see metatag_page_attachments()
//see mettag module
$path = $current_path = \Drupal::service('path.current')->getPath();
$route_name = \Drupal::routeMatch()->getRouteName();
//only works if i edit the view. changes its title
$variables['head_title']['title'] = t('FFFFF');
}
What i now did is hacking the html.html.twig adding this
The custom_title can be added inside the hook_preprocess_html as a variable. Hacky but works.
Here is another solution if you use metatags(who does not?). I post it here because this took me hours to find out.