You are here

function _metatag_remove_duplicate_entity_tags in Metatag 8

Remove duplicate entity tags from a build.

Parameters

array $build: The build.

3 calls to _metatag_remove_duplicate_entity_tags()
metatag_entity_view_alter in ./metatag.module
Implements hook_entity_view_alter().
metatag_page_attachments_alter in ./metatag.module
Implements hook_page_attachments_alter().
_metatag_panelizer_pre_render in ./metatag.module
Pre render callback for entities processed by Panelizer.

File

./metatag.module, line 289
Contains metatag.module.

Code

function _metatag_remove_duplicate_entity_tags(array &$build) {

  // Some entities are built with a link rel="canonical" and/or link
  // rel="shortlink" tag attached.
  // If metatag provides them, remove the ones built with the entity.
  if (isset($build['#attached']['html_head_link'])) {
    $metatag_attachments =& drupal_static('metatag_attachments');
    if (is_null($metatag_attachments)) {

      // Load the meta tags from the route.
      $metatag_attachments = metatag_get_tags_from_route();
    }

    // Check to see if the page currently outputs a canonical and/or shortlink
    // tag.
    if (isset($metatag_attachments['#attached']['html_head'])) {
      foreach ($metatag_attachments['#attached']['html_head'] as $metatag_item) {
        if (in_array($metatag_item[1], [
          'canonical_url',
          'shortlink',
        ])) {

          // Metatag provides rel="canonical" and/or rel="shortlink" tags.
          foreach ($build['#attached']['html_head_link'] as $key => $item) {
            if (isset($item[0]['rel']) && in_array($item[0]['rel'], [
              'canonical',
              'shortlink',
            ])) {

              // Remove the link rel="canonical" or link rel="shortlink" tag
              // from the entity's build array.
              unset($build['#attached']['html_head_link'][$key]);
            }
          }
        }
      }
    }
  }
}