Unverified Commit 64cbadb8 authored by Dave Reid's avatar Dave Reid Committed by Dave Reid

Issue #2941164 by Dave Reid: Fixed multilingual alternate rel links might not...

Issue #2941164 by Dave Reid: Fixed multilingual alternate rel links might not always be present if translated content is also referenced by other links.
parent 7d359e91
......@@ -10,7 +10,6 @@
* Main file for the xmlsitemap module.
*/
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Environment;
use Drupal\Component\Utility\UrlHelper;
......@@ -1807,27 +1806,26 @@ function content_translation_xmlsitemap_element_alter(array &$element, array $li
return;
}
// Check if content translation is enabled for this type. This helps avoid
// the below query for each sitemap link, better for performance.
$enabled = &drupal_static(__FUNCTION__, []);
if (!isset($enabled[$link['type']])) {
/** @var \Drupal\content_translation\ContentTranslationManagerInterface $manager */
$manager = \Drupal::service('content_translation.manager');
try {
$enabled[$link['type']] = $manager->isEnabled($link['type']);
}
catch (PluginNotFoundException $exception) {
// Not a valid entity type.
$enabled[$link['type']] = FALSE;
// @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)) . ')/';
}
}
// Add alternate links for each item's translation.
if ($enabled[$link['type']]) {
if ($loc_whitelist && preg_match($loc_whitelist, $link['loc'])) {
$query = \Drupal::database()->select('xmlsitemap', 'x');
$query->fields('x', ['loc', 'language']);
$query->condition('x.type', $link['type']);
$query->condition('x.id', $link['id']);
$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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment