You are here

function external_hreflang_simple_sitemap_links_alter in External Hreflang 8

Implements hook_simple_sitemap_links_alter().

File

./external_hreflang.module, line 16
Module file.

Code

function external_hreflang_simple_sitemap_links_alter(&$links) {
  $languages = \Drupal::languageManager()
    ->getLanguages();
  foreach ($languages as $language) {
    $tokens_to_process['[current-page:url:relative:' . $language
      ->getId() . ']'] = $language;
  }
  foreach ($links as &$link) {
    if (isset($link['meta'], $link['meta']['path']) && empty($link['meta']['path'])) {
      $tags = _external_hreflang_get_metatags();
      $url = Url::fromRoute('<front>');
    }
    elseif (isset($link['meta'], $link['meta']['entity_info'])) {
      $entity = \Drupal::entityTypeManager()
        ->getStorage($link['meta']['entity_info']['entity_type'])
        ->load($link['meta']['entity_info']['id']);
      if ($entity instanceof EntityInterface) {
        $tags = _external_hreflang_get_metatags($entity
          ->getEntityTypeId(), $entity
          ->bundle());
        $url = Url::fromUserInput('/' . $link['meta']['path']);
      }
    }
    if (!empty($tags)) {
      foreach ($tags as $locale => $path) {
        foreach ($tokens_to_process as $token => $language) {
          if (strpos($path, $token) !== FALSE) {
            $url
              ->setOption('language', $language);
            $href = str_replace('/' . $language
              ->getId(), '', $url
              ->toString());
            $path = str_replace($token, $href, $path);
          }
        }
        $link['alternate_urls'][$locale] = $path;
      }
    }
  }
}