You are here

function content_translation_xmlsitemap_element_alter in XML sitemap 8

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

Implements hook_xmlsitemap_element_alter() for content_translation module.

File

./xmlsitemap.module, line 1882
xmlsitemap XML sitemap

Code

function content_translation_xmlsitemap_element_alter(array &$element, array $link, XmlSitemapInterface $sitemap) {
  if ($link['langcode'] === LanguageInterface::LANGCODE_NOT_SPECIFIED) {
    return;
  }

  // @todo Cache this information longer than a static method.
  $loc_whitelist =& drupal_static(__FUNCTION__);
  if (!isset($loc_whitelist)) {
    $loc_whitelist = FALSE;
    if ($types = array_keys(\Drupal::service('content_translation.manager')
      ->getSupportedEntityTypes())) {
      $query = \Drupal::database()
        ->select('xmlsitemap', 'x');
      $query
        ->distinct(TRUE);
      $query
        ->addExpression('SUBSTRING_INDEX(x.loc, :slash, 2)', 'path', [
        ':slash' => '/',
      ]);
      $query
        ->condition('x.type', $types, 'IN');
      $whitelist_paths = $query
        ->execute()
        ->fetchCol();
      $loc_whitelist = '/^(' . implode('|', array_map(function ($path) {
        return preg_quote($path . '/', '/') . '.*';
      }, $whitelist_paths)) . ')/';
    }
  }
  if ($loc_whitelist && preg_match($loc_whitelist, $link['loc'])) {
    $query = \Drupal::database()
      ->select('xmlsitemap', 'x');
    $query
      ->fields('x', [
      'loc',
      'language',
    ]);
    $query
      ->condition('x.loc', $link['loc']);
    $query
      ->condition('x.language', [
      $link['langcode'],
      LanguageInterface::LANGCODE_NOT_SPECIFIED,
    ], 'NOT IN');
    $query
      ->condition('x.access', 1);
    $query
      ->condition('x.status', 1);
    $language_links = $query
      ->execute();
    while ($language_link = $language_links
      ->fetchAssoc()) {
      _xmlsitemap_element_add_alternate_lang($element, $language_link['loc'], xmlsitemap_language_load($language_link['language']), $sitemap);
    }
  }
}