You are here

function language_query_xmlsitemap_generate_alter in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 xmlsitemap.module \language_query_xmlsitemap_generate_alter()

Implements hook_query_TAG_alter() for language module.

File

./xmlsitemap.module, line 1765
xmlsitemap XML sitemap

Code

function language_query_xmlsitemap_generate_alter(AlterableInterface $query) {
  $mode = \Drupal::config('xmlsitemap.settings')
    ->get('i18n_selection_mode') ?: 'simple';
  if ($mode === 'off') {
    return;
  }

  /** @var \Drupal\xmlsitemap\XmlSitemapInterface $sitemap */
  $sitemap = $query
    ->getMetaData('sitemap');

  // Get languages to simplify query building.
  $default = \Drupal::languageManager()
    ->getDefaultLanguage()
    ->getId();
  $current = isset($sitemap->context['language']) ? $sitemap->context['language'] : $default;
  if ($mode == 'mixed' && $current == $default) {

    // If mode is mixed but current = default, is the same as 'simple'.
    $mode = 'simple';
  }
  switch ($mode) {
    case 'simple':

      // Current language and language neutral.
      $query
        ->condition('language', [
        $current,
        LanguageInterface::LANGCODE_NOT_SPECIFIED,
      ], 'IN');
      break;
    case 'mixed':

      // Mixed current language (if available) or default language (if not) and
      // language neutral.
      $query
        ->condition('language', [
        $current,
        $default,
        LanguageInterface::LANGCODE_NOT_SPECIFIED,
      ], 'IN');
      break;
    case 'default':

      // Only default language and language neutral.
      $query
        ->condition('language', [
        $default,
        LanguageInterface::LANGCODE_NOT_SPECIFIED,
      ], 'IN');
      break;
    case 'strict':

      // Only current language (for nodes), simple for all other types.
      $node_condition = new Condition('AND');
      $node_condition
        ->condition('type', 'node', '=');
      $node_condition
        ->condition('language', $current, '=');
      $normal_condition = new Condition('AND');
      $normal_condition
        ->condition('type', 'node', '<>');
      $normal_condition
        ->condition('language', [
        $current,
        LanguageInterface::LANGCODE_NOT_SPECIFIED,
      ], 'IN');
      $condition = new Condition('OR');
      $condition
        ->condition($node_condition);
      $condition
        ->condition($normal_condition);
      $query
        ->condition($condition);
      break;
    case 'off':

      // All content. No language conditions apply.
      break;
  }
}