You are here

function _xmlsitemap_element_add_alternate_lang in XML sitemap 8

Same name and namespace in other branches
  1. 2.x xmlsitemap.module \_xmlsitemap_element_add_alternate_lang()

Adds alternate language links to a sitemap element.

@internal

Parameters

array $element: The sitemap element from hook_xmlsitemap_element_alter().

string $loc: The location for the URL.

\Drupal\Core\Language\LanguageInterface $language: The alternate language.

\Drupal\xmlsitemap\XmlSitemapInterface $sitemap: The current sitemap being generated for the element.

2 calls to _xmlsitemap_element_add_alternate_lang()
content_translation_xmlsitemap_element_alter in ./xmlsitemap.module
Implements hook_xmlsitemap_element_alter() for content_translation module.
language_xmlsitemap_element_alter in ./xmlsitemap.module
Implements hook_xmlsitemap_element_alter() for language module.

File

./xmlsitemap.module, line 1931
xmlsitemap XML sitemap

Code

function _xmlsitemap_element_add_alternate_lang(array &$element, $loc, LanguageInterface $language, XmlSitemapInterface $sitemap) {
  $url_options = $sitemap->uri['options'];
  $url_options += [
    'absolute' => TRUE,
    'base_url' => rtrim(Settings::get('xmlsitemap_base_url', \Drupal::state()
      ->get('xmlsitemap_base_url')), '/'),
    'alias' => FALSE,
  ];
  $alternate_href = Url::fromUri('internal:' . $loc, [
    'language' => $language,
  ] + $url_options)
    ->toString();
  if ($alternate_href !== $element['loc']) {
    $element[] = [
      'key' => 'xhtml:link',
      'attributes' => [
        'rel' => 'alternate',
        'hreflang' => $language
          ->getId(),
        'href' => $alternate_href,
      ],
    ];
  }
}