Commit 56162d0b authored by Steffen Rühlmann's avatar Steffen Rühlmann Committed by Damien McKenna
Browse files

Issue #2993991 by DamienMcKenna, jcnventura, raphaeltbm, Geolim4, SteffenR,...

Issue #2993991 by DamienMcKenna, jcnventura, raphaeltbm, Geolim4, SteffenR, xurubo93, GuillaumeDuveau, joehuggans, mmaldonado: Metatag_hreflang - duplicate alternate hreflang meta.
parent c4cdd9b3
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -31,6 +31,9 @@ By DamienMcKenna: Minor code readability tweak.
  metatag\Form\MetatagSettingsForm.
#2844696 by DamienMcKenna, Michelle: Allow the field to control whether it is
  displayed in the sidebar or not.
#2993991 by DamienMcKenna, jcnventura, raphaeltbm, Geolim4, SteffenR, xurubo93,
  GuillaumeDuveau, joehuggans, mmaldonado: Metatag_hreflang - duplicate
  alternate hreflang meta.


Metatag 8.x-1.11, 2019-12-20
+30 −0
Original line number Diff line number Diff line
@@ -22,3 +22,33 @@ function metatag_hreflang_help($route_name, RouteMatchInterface $route_match) {
    default:
  }
}

/**
 * Implements hook_page_attachments_alter().
 */
function metatag_hreflang_page_attachments_alter(array &$attachments) {
  // Only bother doing anything if both the "html_head" and "html_head_link"
  // structures are present in the output.
  if (!empty($attachments['#attached']['html_head'])) {
    if (!empty($attachments['#attached']['html_head_link'])) {
      // Get all defined hreflang_per_language values from html_head.
      $hreflang_per_language = [];
      foreach ($attachments['#attached']['html_head'] as $element) {
        // Check for Metatag's identifier "hreflang_per_language".
        if (!empty($element[1])) {
          if (strpos($element[1], 'hreflang_per_language') !== false) {
            $hreflang_per_language[] = $element[0]['#attributes']['hreflang'];
          }
        }
      }

      // Remove default links coming from content_translation if already defined
      // by Metatag.
      foreach ($attachments['#attached']['html_head_link'] as $key => $element) {
        if (isset($element[0]['hreflang']) && in_array($element[0]['hreflang'], $hreflang_per_language)) {
          unset($attachments['#attached']['html_head_link'][$key]);
        }
      }
    }
  }
}