Is it possible in this module to implement a solution for the following problem:
https://www.drupal.org/node/2625300
I think it will be important for many.

CommentFileSizeAuthor
#62 metatag-unset-2735195.patch10.29 KBcodepress
#59 metatag-unset-2735195-59.patch10.45 KBpiotrkonefal
#55 metatag-unset-2735195-55.patch10.89 KBjoelstein
#51 metatag-n2735195-51.patch8.45 KBDamienMcKenna
#48 metatag-n2735195-48.patch6.54 KBDamienMcKenna
#47 metatag-n2735195-47.patch6.57 KBDamienMcKenna
#45 metatag-unset-2735195-45.patch10.39 KBSimon Georges
#42 interdiff_38-42.txt1.53 KBsonnykt
#42 metatag-unset-2735195-42.patch10.29 KBsonnykt
#41 interdiff_38-41.txt2 KBsonnykt
#41 metatag-unset-2735195-41.patch10.82 KBsonnykt
#40 interdiff_38-40.txt2 KBsonnykt
#40 metatag-unset-2735195-40.patch10.82 KBsonnykt
#39 metatag-unset-2735195-38.patch8.57 KBhugovk
#37 metatag-unset-2735195-37.patch9.2 KBwroxbox
#36 metatag-unset-2735195-36.patch8.56 KBphjou
#35 metatag-unset-2735195-6.patch8.86 KBjoelstein
#32 2735195-metatag-unset-5.patch9.35 KBWalkingDexter
#30 2735195-metatag-unset-4.patch9.1 KBWalkingDexter
#27 2735195-metatag-unset-3.patch9.09 KBWalkingDexter
#25 2735195-metatag-unset-2.patch9.08 KBWalkingDexter
#22 2735195-metatag-unset.patch9.06 KBWalkingDexter
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

fomenkoandrey created an issue. See original summary.

DamienMcKenna’s picture

fomenkoandrey’s picture

drupal 8.1.8 - not fixed.

DamienMcKenna’s picture

@fomenkoandrey: Yes, because the last release of Drupal 8 was on August 4th but the fix only went in on August 18th. The fix will be in the next core release.

fomenkoandrey’s picture

8.1.9 - not fixed

DamienMcKenna’s picture

Status: Closed (duplicate) » Active

Here are the meta tags that 8.2.x currently output:

<meta name="Generator" content="Drupal 8 (https://www.drupal.org)" />
<meta name="MobileOptimized" content="width" />
<meta name="HandheldFriendly" content="true" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="/core/misc/favicon.ico" type="image/vnd.microsoft.icon" />
<link rel="canonical" href="/node/8" />
<link rel="shortlink" href="/node/8" />
<link rel="revision" href="/node/8" />

Lets add an option to forcibly remove the link tags.

fomenkoandrey’s picture

I understand right: modification in the core to wait only 8.2 branch?
on the main site I have not yet removed the extension Unset HTML head link.
I do not understand what and where I need to add an option?
I think, it is better to wait a core update.

DamienMcKenna’s picture

The core change removed some of the additional meta tags that pointed at admin-only paths, the remaining ones are not bad, but obviously people want to have control over them.

fomenkoandrey’s picture

today updated to 8.2 and still have all discussed metatags

DamienMcKenna’s picture

Yes, because we haven't added anything to remove the extra meta tags. Yet.

fomenkoandrey’s picture

thank U, will wait update then.

fomenkoandrey’s picture

DamienMcKenna, these changes were not added in the last update?

DamienMcKenna’s picture

No patches have been uploaded by others, and s far I've focused on other tasks, so no, it hasn't been done yet. When someone works on a patch to make this change you'll find out because it'll be uploaded here and you're subscribed to notifications for this issue.

DamienMcKenna’s picture

Status: Active » Postponed

Marking this "postponed" as I won't be looking at it until after 1.0.

DamienMcKenna’s picture

Status: Postponed » Active

8.x-1.0 is out, so this is fair game again.

thejimbirch’s picture

Ran into this again today and did some digging. It looks like these tags are set in Drupal 8 core's system.module

/**
 * Implements hook_page_attachments().
 *
 * @see template_preprocess_maintenance_page()
 * @see \Drupal\Core\EventSubscriber\ActiveLinkResponseFilter
 */
function system_page_attachments(array &$page) {
  // Ensure the same CSS is loaded in template_preprocess_maintenance_page().
  $page['#attached']['library'][] = 'system/base';
  if (\Drupal::service('router.admin_context')->isAdminRoute()) {
    $page['#attached']['library'][] = 'system/admin';
  }

  // Attach libraries used by this theme.
  $active_theme = \Drupal::theme()->getActiveTheme();
  foreach ($active_theme->getLibraries() as $library) {
    $page['#attached']['library'][] = $library;
  }

  // Attach favicon.
  if (theme_get_setting('features.favicon')) {
    $favicon = theme_get_setting('favicon.url');
    $type = theme_get_setting('favicon.mimetype');
    $page['#attached']['html_head_link'][][] = [
      'rel' => 'shortcut icon',
      'href' => UrlHelper::stripDangerousProtocols($favicon),
      'type' => $type,
    ];
  }

  // Get the major Drupal version.
  list($version, ) = explode('.', \Drupal::VERSION);

  // Attach default meta tags.
  $meta_default = [
    // Make sure the Content-Type comes first because the IE browser may be
    // vulnerable to XSS via encoding attacks from any content that comes
    // before this META tag, such as a TITLE tag.
    'system_meta_content_type' => [
      '#tag' => 'meta',
      '#attributes' => [
        'charset' => 'utf-8',
      ],
      // Security: This always has to be output first.
      '#weight' => -1000,
    ],
    // Show Drupal and the major version number in the META GENERATOR tag.
    'system_meta_generator' => [
      '#type' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => [
        'name' => 'Generator',
        'content' => 'Drupal ' . $version . ' (https://www.drupal.org)',
      ],
    ],
    // Attach default mobile meta tags for responsive design.
    'MobileOptimized' => [
      '#tag' => 'meta',
      '#attributes' => [
        'name' => 'MobileOptimized',
        'content' => 'width',
      ],
    ],
    'HandheldFriendly' => [
      '#tag' => 'meta',
      '#attributes' => [
        'name' => 'HandheldFriendly',
        'content' => 'true',
      ],
    ],
    'viewport' => [
      '#tag' => 'meta',
      '#attributes' => [
        'name' => 'viewport',
        'content' => 'width=device-width, initial-scale=1.0',
      ],
    ],
  ];
  foreach ($meta_default as $key => $value) {
    $page['#attached']['html_head'][] = [$value, $key];
  }

  // Handle setting the "active" class on links by:
  // - loading the active-link library if the current user is authenticated;
  // - applying a response filter if the current user is anonymous.
  // @see \Drupal\Core\Link
  // @see \Drupal\Core\Utility\LinkGenerator::generate()
  // @see template_preprocess_links()
  // @see \Drupal\Core\EventSubscriber\ActiveLinkResponseFilter
  if (\Drupal::currentUser()->isAuthenticated()) {
    $page['#attached']['library'][] = 'core/drupal.active-link';
  }
}

I'm not sure you how you would add an option to Metatag to remove, but looks like there is a custom module idea to remove them.

https://github.com/koddr/drupal_8_unset_html_head_link

From discussion at:
https://www.drupal.org/node/2625300

thejimbirch’s picture

Title: Unset link rel: shortlink, edit-form, version-history and revision from <head> » Unset link rel: shortlink, edit-form, version-history and revision, and meta name: Generator, MobileOptimized, HandheldFriendly, viewport from <head>

In comment #6 DamienMcKenna added in the meta tags that core is adding, not just the links, so I updated the title.

I found a test in core that had a nice hook that can be used in a custom module to remove the metatags.

/**
 * Implements hook_page_attachments_alter().
 */
function my_module_page_attachments_alter(&$page) {
  // Remove the HTML5 mobile meta-tags.
  $meta_tags_to_remove = [
    'system_meta_content_type',
    'system_meta_generator',
    'MobileOptimized',
    'HandheldFriendly',
    'viewport'
  ];
  // Unset loop.
  foreach ($page['#attached']['html_head'] as $index => $parts) {
    if (in_array($parts[1], $meta_tags_to_remove)) {
      unset($page['#attached']['html_head'][$index]);
    }
  }
}
thejimbirch’s picture

I made a custom module that removes these Metatags which can be used until this functionality can be built into the main Metatag module.

https://github.com/xenomedia/metatag_remove

This does not solve the issue as this module just completely removes them, does not provide options for removing them individually in the admin interface.

DarkstarTom’s picture

The module in #18 correctly removes the meta tags from the header but how do you remove the link tags such as:

<link rel="shortlink" href="/node/8" />
<link rel="revision" href="/node/8" />

Thanks!

thejimbirch’s picture

@DarkstarTom I posted a bit about this at the end of the post in #16

I believe this module does that:
https://github.com/koddr/drupal_8_unset_html_head_link

And the issue that you already found:
https://www.drupal.org/node/2625300

DarkstarTom’s picture

@thejimbirch thanks, the drupal_8_unset_html_head_link module does work for content but I am trying to use it on a profile page created with the profile module and it isn't removing the link. I would love to have this feature in Metatag.

WalkingDexter’s picture

I created a patch to fix problem from this issue. Patch adds metatag_unset sub-module, that allows to remove meta tags by "rel" (<link rel="value">) and "name" (<meta name="value">) attributes. Part of the code was taken from the https://github.com/koddr/drupal_8_unset_html_head_link.

How to use metatag_unset module:

  1. Enable the module
  2. Go to admin/config/search/metatag/unset configuration page
  3. Enable unset type and configure "rel" or/and "name" attribute values
  4. Rebuild site cache
  5. That's all - meta tags with the specified attribute values will be removed from the head section

Please test this patch.

WalkingDexter’s picture

Status: Active » Needs review
DamienMcKenna’s picture

Status: Needs review » Needs work

This needs to be part of the main module vs a submodule.

WalkingDexter’s picture

Status: Needs work » Needs review
FileSize
9.08 KB

Made a patch, where Unset metatags functional is part of the main module.

Status: Needs review » Needs work

The last submitted patch, 25: 2735195-metatag-unset-2.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

WalkingDexter’s picture

Status: Needs work » Needs review
FileSize
9.09 KB

Fix ConfigSchemaChecker errors.

WidgetsBurritos’s picture

Status: Needs review » Needs work

I've tested the patch in #27. The functionality definitely works, but I have some feedback about the overall strategy and some minor nitpicks.

  1. +++ b/metatag.routing.yml
    @@ -48,3 +48,13 @@ entity.metatag_defaults.revert_form:
    +metatag.unset:
    +  path: '/admin/config/search/metatag/unset'
    +  defaults:
    +    _form: '\Drupal\metatag\Form\MetatagUnsetForm'
    +    _title: 'Unset metatags'
    +  requirements:
    +    _permission: 'administer meta tags'
    

    Does it make sense for this to live at global level, or should it be available per configuration group? There may be reasons for these tags to exist in certain contexts.

  2. +++ b/metatag.module
    @@ -207,6 +211,36 @@ function _metatag_remove_duplicate_entity_tags(array &$build) {
    +function _metatag_unset_specified_tags(array &$build) {
    +  $config = \Drupal::config('metatag.unset');
    +  // Unset head link elements by "rel" attribute.
    +  if (isset($build['#attached']['html_head_link']) && $config->get('unset_by_rel') && $values = $config->get('rel_list')) {
    +    $html_head_links = &$build['#attached']['html_head_link'];
    +    foreach ($html_head_links as $key => $link) {
    +      if (isset($link[0]['rel']) && in_array($link[0]['rel'], $values)) {
    +        unset($html_head_links[$key]);
    +      }
    +    }
    +  }
    

    Should this be a little bit more restrictive and only remove "rel" from <link> tags like its doing with the meta name tags? Not that anyone should have any <a> or <area> tags in their head, but merely from the perspective of being precise I think it makes sense.

  3. +++ b/src/Form/MetatagUnsetForm.php
    @@ -0,0 +1,127 @@
    +    // Unset by "rel" attribute.
    +    $form['unset_by_rel'] = [
    +      '#type' => 'checkbox',
    +      '#title' => $this->t('Unset meta tags by "rel" attribute'),
    +      '#default_value' => $config->get('unset_by_rel'),
    +    ];
    +    // Values list for "rel" attribute.
    +    $form['rel_list'] = [
    +      '#type' => 'textarea',
    +      '#description' => $this->t('List of "rel" attribute values (one value per line). Meta tags with a "rel" attribute that has one of these values will be removed.'),
    

    This should probably say "link tags" instead of "meta tags" to avoid confusion.

  4. +++ b/metatag_views/metatag_views.links.task.yml
    @@ -1,8 +1,3 @@
    -metatag_views.defaults:
    -  title: 'Defaults'
    -  route_name: entity.metatag_defaults.collection
    -  base_route: entity.metatag_defaults.collection
    -  weight: 0
    

    What is the intention for removing this here? I'm not seeing its relevance.

  5. +++ b/src/Form/MetatagUnsetForm.php
    @@ -0,0 +1,127 @@
    +      '#default_value' => implode("\n", $rel_list),
    
    +++ b/src/Form/MetatagUnsetForm.php
    @@ -0,0 +1,127 @@
    +      '#default_value' => implode("\n", $name_list),
    
    +++ b/src/Form/MetatagUnsetForm.php
    @@ -0,0 +1,127 @@
    +      $rel_list = explode("\n", $form_state->getValue('rel_list', ''));
    
    +++ b/src/Form/MetatagUnsetForm.php
    @@ -0,0 +1,127 @@
    +      $name_list = explode("\n", $form_state->getValue('name_list', ''));
    

    If we go this route, I'd imagine we'd want to use PHP_EOL instead of \n in these instances.

  6. Whatever the outcome of this issue is, it's gonna need test coverage.
WidgetsBurritos’s picture

Disregard comment #28.2 above. I just realized that the "html_head_link" key, is what determines it's a link tag.

WalkingDexter’s picture

Status: Needs work » Needs review
FileSize
9.1 KB

Hi WidgetsBurritos!

Below are the answers to your remarks:
1. For the moment, I see no reason to divide the metatag.unset configuration by the context. The current configuration, which is presented in the patch, is aimed at solving a problem that is global in nature. I don't exclude that in the future, it may be necessary to divide metatag.unset configuration per group, but, I repeat, at the moment I see no reason to do it.
3. I don't think that it's a good idea to say "link tags", because this issue also tells about removing tags with the names Generator, MobileOptimized, HandheldFriendly, viewport, which are meta tags.
4. The metatag_views.links.task.yml file is written incorrectly, since it defines the base local task - metatag_views.defaults. Because of this, the metatag.tab_unset tab will not work if the metatag_views module is disabled. The definition of the local task should be in the metatag.links.task.yml file.
5. I agree on this point, made the changes in the patch below.

DamienMcKenna’s picture

Status: Needs review » Needs work

This is really great work, thank you.

A small request: instead of having one settings page for the "unset" options, could it be slightly refactored to be a general settings page for the module with a section focused on the unset options? Thanks.

WalkingDexter’s picture

Status: Needs work » Needs review
FileSize
9.35 KB

Yep, it can be done! Added a new patch.

DamienMcKenna’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Thanks @WalkingDexter, I really appreciate your work on this.

That said, I think this should be able to automatically handle core's meta tags without any additional effort for the site builder - it should just work. I'm still wondering how best to handle this.

Phonoman’s picture

Hey, I was trying to use this (#30 & #32) patch with composer, but it couldn't be applied (dev-1.x/1.5)?
Perhaps some of the files used here have been changed thus rendering the patch unusable?

joelstein’s picture

Here's a patch which applies cleanly against 8.x-1.5 and 8.x-1.x-dev.

phjou’s picture

Ok I've rerolled the patch.

There are some changes on the dev branch that have broken the patch. In particular this issue: https://www.drupal.org/project/metatag/issues/2956199 so I've deleted the changes made in /metatag_views/metatag_views.links.task.yml

wroxbox’s picture

Status: Needs work » Needs review
FileSize
9.2 KB

Rerolling the patch against 1.7/head

Status: Needs review » Needs work

The last submitted patch, 37: metatag-unset-2735195-37.patch, failed testing. View results

hugovk’s picture

Status: Needs work » Needs review
FileSize
8.57 KB

Rerolled the patch against 1.8/head.

sonnykt’s picture

Patch #39 does not unset the rel="revision" from node pages. Those rel links are set at the end of NodeViewController:view(), not from entity_view() nor hook_page_attachments(). I added an event subscriber listening to the KernelEvents::VIEW to unset those links from the controller result.

sonnykt’s picture

Fixed #40 with correct weighting.

sonnykt’s picture

Sorry but just found that patch #40 and #41 introduce an incorrect config file metatag.unset.yml in the root dir. Fixed in patch #42.

govind.maloo’s picture

Status: Needs review » Reviewed & tested by the community

@sonnykt for patch
Patch is working fine and resolved the issue.
Also successfully tested and deployed on https://github.com/govCMS/govCMS8

DamienMcKenna’s picture

Status: Reviewed & tested by the community » Needs work

Going back to my request from earlier:

I think this should be able to automatically handle core's meta tags without any additional effort for the site builder - it should just work.

I think the best way forward would be to extend _metatag_remove_duplicate_entity_tags() to compare known meta tags from core and Metatag and delete the core ones - the list is small, so this should be doable.

Simon Georges’s picture

FileSize
10.39 KB

Re-roll of the patch for Metatag 1.11 version (credits to original authors of the patch). Not changing the status because this is just a re-roll and does NOT take DamienMcKenna's request in #44 into account.

fy1128’s picture

got an error with restful request after metatag-unset-2735195-45.patch applied.

TypeError:Argument 1 passed to _metatag_unset_specified_tags() must be of the type array, object given, called in /var/www/html/web/modules/contrib/metatag/src/EventSubscriber/EntityViewSubscriber.php on line 32 at function _metatag_unset_specified_tags() (file /var/www/html/web/modules/contrib/metatag/metatag.module 293 line)#0 /var/www/html/web/modules/contrib/metatag/src/EventSubscriber/EntityViewSubscriber.php(32): _metatag_unset_specified_tags(Object(GuzzleHttp\Psr7\Response)) #1 [internal function]

DamienMcKenna’s picture

Status: Needs work » Needs review
FileSize
6.57 KB

This is the kind of thing I was thinking of.

One thing I noticed is that in when the event subscriber runs the $build['#attached']['html_head'] values aren't defined, so I'm not sure where to remove those.

DamienMcKenna’s picture

I accidentally left in a kint() call in #47.

Status: Needs review » Needs work

The last submitted patch, 48: metatag-n2735195-48.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

DamienMcKenna’s picture

Core's "Generator" HTTP value comes form Drupal\Core\EventSubscriber\ResponseGeneratorSubscriber, while the meta tags are added in system_page_attachments().. so maybe an EventSubscriber isn't the best approach..

DamienMcKenna’s picture

How about disabling system_page_attachments() entirely? :)

DamienMcKenna’s picture

Status: Needs work » Needs review

#52 has unintended consequences and most of the theme doesn't load :-\

Status: Needs review » Needs work

The last submitted patch, 51: metatag-n2735195-51.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

hugovk’s picture

A reroll of #45 is needed for latest metatag 8.x-1.13.

joelstein’s picture

Here's an updated patch from #45 that applies cleanly.

DamienMcKenna’s picture

Thanks Joel. Seems like you accidentally included some unwanted files, though.

Muath Khraisat’s picture

This way will unset the links from both the HTML and the HTTP headers.
Is there a way to unset them from one method only and keep them via the other?

JulienVey’s picture

Is it still ocurring in the latest version of the metatag module : 1.16.0 ? Do we still need this patch ?

piotrkonefal’s picture

Status: Needs work » Needs review
FileSize
10.45 KB

Here is a rerolled version for the latest 8.x-1.16.

JulienVey’s picture

Much appreciated man.

DamienMcKenna’s picture

Status: Needs review » Closed (won't fix)

This problem has been resolved in core: https://www.drupal.org/node/3216941

codepress’s picture

FileSize
10.29 KB

If you are still on 9.2.x, patch against 1.19 is attached.

wsantell’s picture

I am on 9.3.12 and I notice that these tags are still present:
MobileOptimized, HandheldFriendly, viewport

Generator is also present, and can't be overridden or unset - if you set "generator" in the metatag settings, it will add a separate link, so you will have 2 entries like this:

<meta name="generator" content="[Setting from Metatag]">
<meta name="Generator" content="Drupal 9 (https://www.drupal.org)">

Reviewing the resolution in #61, they only removed the delete-form, delete-multiple-form, edit-form, version-history, and revision links. It did say shortlink was still included by default, but no shortlink is added when using metatag and this isn't set.

Applying the patch in #62 does resolve this once configured.

jedihe’s picture

I'm seeing the same bug as #63, even with the current 1.x-dev version (e807dc86cf8fb87fd8c302161dcf89b1f2f6fea7). I suggest to re-open this ticket or create a new one to handle cases like 'Generator', etc.

In the meantime, the hook_page_attachments_alter() approach from #62 works for me (I ported only the unset-by-name part into a custom module in my current project).