3

I have created a custom module for displaying the players information. Module will create dynamic pages and each page will display players stats. With the help of the following links, I was able to add meta tags to my module.

  1. How to add meta tag in head
  2. Adding meta tags

Since each of the page generated by my module for separate meta information, I was not able to use page_attachement_hooks. Using the above links, I was able to add meta information in my controller itself.

But, I am using Meta tag drupal module for generating meta information for other pages. Now I am getting multiple meta tags with same property name. Global configurations of meta tag module is displaying along with my meta tags.

I read in some blog that if multiple meta tags found with same property, few crawlers will ignore them. How do I keep only my meta tags?

I saw this post which says how to override the meta tags. But there the code has to be placed in the module and it will be static. I wanted dynamic meta tags for each page. How will I achieve it?

Here is my code.

PlayerController.php

<?php

namespace Drupal\player\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal;

class PlayerController extends ControllerBase {

  public function content($player) {
    $result = [];
    /* --------------
     code logic here 
    ------------------ */
    return [
      '#type' => 'markup',
      '#markup' => $result,
      '#attached' => [
        'html_head' => [
        [
         [
          '#tag' => 'meta',
          '#attributes' => [
            'property' => 'og:description',
            'content' => $result->player_details,
          ],
        ],
        'player',
      ],
    ],
  ],
    ];
  }

}

For reference, I am adding link to my old question regarding how to add meta tags in dynamic module. It have more codes in case you need.

3

Edit – @AlfredArmstrong pointed me to the hooks provided by the Metatag module itself. And there's hook_metatags_alter() you can implement to completely disable all meta tags added by the Metatag module. Much shorter than my original answer.

/**
 * Implements hook_metatags_alter().
 */
function MYMODULE_metatags_alter(array &$metatags, array &$context) {

  // Exclude meta tags on 'player' route.
  if (\Drupal::routeMatch()->getRouteName() === 'player') {
    $metatags = NULL;
  }
}

Original answer – I've had similar problems before. You can override or unset meta tags added by the Metatag module implementing hook_page_attachments_alter() like in the following example.

/**
 * Implements hook_page_attachments_alter().
 */
function MYMODULE_page_attachments_alter(array &$attachments) {

  // Check for the route you want to override.
  if (\Drupal::routeMatch()->getRouteName() === 'player') {

    // An array of tags you want to unset.
    $unset = [
      'description' => 1,
      'abstract' => 1,
      'keywords' => 1,
    ];

    $meta_tags = $attachments['#attached']['html_head'];
    foreach ($meta_tags as $key => $meta_tag) {

      // And now unset them.
      if (isset($unset[$meta_tag[1]])) {

        unset($attachments['#attached']['html_head'][$key]);
      }
    }
  }
}
  • 1
    Is there some reason not to use a metatag hook? git.drupalcode.org/project/metatag/blob/8.x-1.x/metatag.api.php Looks like hook_metatags_attachments_alter() would be the one to use. – Alfred Armstrong Nov 11 at 15:40
  • @AlfredArmstrong – Oh yes, I thought there must be some hook for that. But I didn't look thoroughly enough. I will check that out later and update my answer accordingly. Or you provide that in a new answer. – leymannx Nov 11 at 16:14
  • No, go ahead, though would also be worth pointing out that the hook implementation could be used to set the desired values rather than removing the ones from metatag. Might be cleaner. – Alfred Armstrong Nov 11 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.