_metatag_remove_duplicate_entity_tags() fails for entities

Created on 1 July 2025, 3 days ago

Problem/Motivation

_metatag_remove_duplicate_entity_tags(array &$build) is used to remove rel="canonical" and/or rel="shortlink" link tags built by the entities itself if those are provided by metatag, to prevent duplicates. This still might work for taxonomy terms via metatag_page_attachments_alter() executed as last page attachments alteration, but calling it from metatag_entity_view_alter() doesn't work anymore since the links generated by Drupal\Core\Entity\Controller\EntityViewController::view() are added just before the final rendering via bubblable metadata, which cannot be catched and removed by either of the hooks mentioned above.

Duplicated rel="canonical" and/or rel="shortlink" have a huge negative SEO impact on production sites, therefore setting the priority to major.

Steps to reproduce

  • Install an enable the Metatag module on a Drupal site.
  • Configure it to provide the canonical and shortlink tags.
  • Create a node and view it. Check the HTML output. It should contain only one link tag of each but contains two.

Proposed resolution

I don't know if this can be fixed without modifications to the core, so unfortunately I have no idea how to solve this. #2922570 is a starting point to search where it comes from.

🐛 Bug report
Status

Active

Version

2.1

Component

Code

Created by

🇦🇹Austria mvonfrie

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024