You are here

function metatag_hreflang_page_attachments_alter in Metatag 8

Implements hook_page_attachments_alter().

File

metatag_hreflang/metatag_hreflang.module, line 29
Contains metatag_hreflang.module.

Code

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]);
        }
      }
    }
  }
}