Remove sitemap items of disabled languages

Created on 28 November 2019, over 5 years ago
Updated 21 November 2023, over 1 year ago

Hi,

I am using this module on my site together with the Simple XML Sitemap module (simple_sitemap) and also the Domain Simple Sitemap module (domain_simple_sitemap) to generate a sitemap for my project for each domain.

The sitemaps (sitemap.xml) contain links in languages that are disabled for that domain via this module (Domain Language Access).
It would be nice if there is a way to remove items from a sitemap if the language is disable via Domain Language Access.

I have created a workaround for myself that does this in a custom module:

/**
 * Implements hook_simple_sitemap_links_alter().
 *
 * Alter sitemap generating:
 * Remove language links if language is not activated on the domain.
 */
function mymodule_simple_sitemap_links_alter(array &$links, $sitemap_variant) {
  $domain_languages = _mymodule_get_domain_languages($sitemap_variant);

  foreach ($links as $key => $link) {

    // Remove item from sitemap if the items language is not in the domains languages.
    if (!in_array($link['langcode'], $domain_languages)) {
      unset($links[$key]);
      continue;
    }

    // Remove alternate urls if its language is not in the domains languages.
    if (isset($link['alternate_urls']) && !empty($link['alternate_urls'])) {
      foreach ($link['alternate_urls'] as $langcode => $alternate_url) {
        if (!in_array($langcode, $domain_languages)) {
          unset($links[$key]['alternate_urls'][$langcode]);
        }
      }
    }
  }
}

/**
 * Get an array of language codes activated for a domain.
 *
 * @param string $domain_key
 *   Domain machine name.
 *
 * @return array
 *   An array of language codes that are enabled for the domain.
 */
function _mymodule_get_domain_languages($domain_key) {
  $config = \Drupal::config('domain.language.' . $domain_key . '.language.negotiation')->getRawData();
  if (isset($config['languages'])) {
    if (isset($config['languages']['***LANGUAGE_site_default***'])) {
      unset($config['languages']['***LANGUAGE_site_default***']);
    }
    return $config['languages'];
  }
  $languages = [];
  $languages[] = \Drupal::languageManager()->getDefaultLanguage()->getId();
  return $languages;
}

Feature request
Status

Needs review

Version

2.0

Component

Code

Created by

🇧🇪Belgium flyke

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024