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

delta created an issue. See original summary.

delta’s picture

Issue summary: View changes
joelpittet’s picture

Version: 8.0.3 » 8.0.x-dev

I'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.

delta’s picture

question answered

delta’s picture

In theme.inc preprocess_html :

If I change the line $variables['head_title'] = $head_title; to

    $variables['#attached']['html_head'][] = [array('#tag' => 'title', '#value' => implode(' | ', $head_title)), 'title'];

and 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 :/

delta’s picture

Issue summary: View changes
delta’s picture

Issue summary: View changes
delta’s picture

Issue summary: View changes
delta’s picture

Issue summary: View changes
delta’s picture

Issue summary: View changes
delta’s picture

delta’s picture

Issue summary: View changes

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
Lendude’s picture

This is how the metatag module handles this:

    foreach ($metatag_attachments['#attached']['html_head'] as $item) {
      // Do not attach a title meta tag as this unnecessarily duplicates the
      // title tag.
      // @see metatag_preprocess_html()
      if ($item[1] == 'title') {
        continue;
      }

And then metatag_preprocess_html() does this:

  // Copy the "title" meta tag into the regular <title> tag. The redundant meta
  // tag is never added to $variables['#attached'].
  // @see metatag_page_attachments()
  if (!empty($metatag_attachments['#attached']['html_head'])) {
    foreach ($metatag_attachments['#attached']['html_head'] as $attachment) {
      if (!empty($attachment[1]) && $attachment[1] == 'title') {
        // Empty head_title to avoid the site name and slogan to be appended to
        // the meta title.
        $variables['head_title'] = [];
        $variables['head_title']['title'] = html_entity_decode($attachment[0]['#attributes']['content'], ENT_QUOTES);

        // No need to do anything else after this.
        break;
      }
    }
  }

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

marcoka’s picture

hook_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

  {% if tests_title|default %}
      <title>{{ custom_title }}</title>
    {% else %}
      <title>{{ head_title|safe_join(' | ') }}</title>
    {% endif %}

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.

function MYMODULE_metatags_attachments_alter(array &$attachments) {

  //source: https://drupalium.com/articles/override-head-title-set-by-metatag-module

  if (!empty($attachments['#attached']['html_head'])) {
    foreach ($attachments['#attached']['html_head'] as $key => $attachment) {
      if (!empty($attachment[1]) && $attachment[1] == 'title') {

        // Here we set our new head title.      
        $attachments['#attached']['html_head'][$key][0]['#attributes']['content'] = 'MY TITLE';
        break;
      }
    }
  }
}

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.