Prevent global metatags on a custom route

Created on 21 November 2024, about 14 hours ago

Problem/Motivation

I have a custom module which serves up some content at a custom route /spotlight/{id} using a Controller.

I am adding meta tags with render arrays to the [#attached][html_head] portion of a #theme render array.

I still see the global metatags being attached before my meta tags, so I see duplicates of things like og:description, and social media is using the global one rather than the one I am adding.

How do I stop the global metatags from being added on a route?

I've tried preprocess_page, metatags_alter, metatags_attachments_alter, but nothing lets me interrupt or remove those metatags.

πŸ’¬ Support request
Status

Active

Version

2.0

Component

Integration with other module

Created by

πŸ‡ΊπŸ‡ΈUnited States nessthehero

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

Comments & Activities

  • Issue created by @nessthehero
  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    Try using hook_metatag_entity_type_is_supported_alter(), please let us know if that works.

  • πŸ‡ΊπŸ‡ΈUnited States nessthehero

    @damienmckenna Do you have more info on that hook? I can't find it documented anywhere and I can't pause inside it with XDebug.

    Just updated my local metatag to 2.1.0 as well.

  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    Oh, sorry, that's from D7.

    You could try hook_metatag_route_entity(), please see if the current route is your custom entity type and return NULL.

  • πŸ‡ΊπŸ‡ΈUnited States nessthehero

    Unfortunately I'm not seeing any results from this. Returning null doesn't change any of the global metatags that are pulled into the page.

  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    It might be necessary to add a new hook that allows disabling Metatag entirely for a route.. or maybe extend Metatag Custom Routes to allow this as an option?

  • πŸ‡ΊπŸ‡ΈUnited States nessthehero

    Just to give a sampling of what I've tried so far:

    function cmu_expertfile_metatag_route_entity() {
      if (strpos(\Drupal::routeMatch()->getRouteName(), 'cmu_expertfile') !== FALSE) {
        return [];
      }
    }
    
    function cmu_expertfile_metatags_alter(array &$metatags, array $context) {
      $metatags['og:description'] = '-2- Beep boop website stuff';
    }
    
    function cmu_expertfile_metatags_attachments_alter(array &$attachments) {
      if (isset($attachments['#attached']['html_head'])) {
        foreach ($attachments['#attached']['html_head'] as $key => &$attachment) {
          if ($attachment[1] == 'og:description') {
            $attachments['#attached']['html_head'][$key][0]['#attributes']['content'] = 'Beep boop website stuff';
          }
        }
      }
    }
    
    function cmu_expertfile_page_attachments_alter(array &$page) {
      if (!empty($page['#attached']['html_head'])) {
        foreach ($page['#attached']['html_head'] as &$attachment) {
          if ($attachment[1] == 'og:description') {
            $attachment[0]['#attributes']['content'] = 'Beep boop website stuff';
    
          }
        }
      }
    }
    

    I'm just trying to get literally anything to show up in the Global og:description meta tag from my custom module, and I can't override it this way.

    I would expect that adding a metatag to html_head would at some point reduce any duplicates, deferring to the most recent attachment. If the metatag module is set up to add Global metatags last, I would also then expect that module to detect existing items first.

  • πŸ‡ΊπŸ‡ΈUnited States nessthehero

    It might be necessary to add a new hook that allows disabling Metatag entirely for a route.. or maybe extend Metatag Custom Routes to allow this as an option?

    Yes, this sounds promising. Happy to help in any way I can.

  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    Alternatively, have you considered using Metatag Custom Routes to control the meta tags you want to show?

  • πŸ‡ΊπŸ‡ΈUnited States nessthehero

    That module is too experimental for our use case. I also feel like what I'm trying to do should work out of principle. It doesn't seem that far out there to just be able to override things in PHP.

  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    Take a look at ✨ Add placeholder to clear / unset inherited value Closed: duplicate , it might help.

Production build 0.71.5 2024