I am adding a viewport tag from within my contrib theme using hook_page_attachments_alter but core's does not get overridden so I end up with two viewport tags, one from my theme and the other from core. The viewport tag from core seems to come from:

class DefaultHtmlFragmentRenderer implements HtmlFragmentRendererInterface

in

/core/lib/Drupal/Core/Page/DefaultHtmlFragmentRenderer.php

($page->addMetaElement(new MetaElement(NULL, array('name' => 'viewport', 'content' => 'width=device-width, initial-scale=1.0')));)

I add my custom viewport meta tag like this in my theme's .theme file:

function MYTHEME_page_attachments_alter(array &$page) {
    $viewport = array(
      '#type' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => array(
        'name' => 'viewport',
        'content' => 'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no',
      ),
    );
    $page['#attached']['html_head'][] = [$viewport, 'viewport'];
  }

It ends up looking like this:
Two viewport tags

CommentFileSizeAuthor
tags.png84.75 KBdanny englander

Comments

berdir’s picture

I would suggest to wait for #2352155: Remove HtmlFragment/HtmlPage or try the patch there before trying to do anything here. That will probably change a lot related to all this.

danny englander’s picture

@Berdir - thanks for all your help and link to that issue.

danny englander’s picture

Status: Active » Fixed

This is now fixed in Beta 4.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.