Commit bdf94567 authored by gbyte.co's avatar gbyte.co

Add all localised URLs of each path as locations and add every localised URL...

Add all localised URLs of each path as locations and add every localised URL of each location as alternate to each location... :P
parent 3c1d5959
......@@ -186,34 +186,27 @@ class Batch {
$url_object->setOption('absolute', TRUE);
$urls = [];
foreach ($languages as $language) {
$langcode = $language->getId();
// Exclude untranslated paths.
if ($batch_info['skip_untranslated']) {
if ($language->isDefault() || $entity->hasTranslation($langcode)) {
$url_object->setOption('language', $language);
$urls[$langcode] = $url_object->toString();
}
}
else {
$url_object->setOption('language', $language);
$urls[$langcode] = $url_object->toString();
}
}
$context['results']['generate'][] = [
$path_data = [
'path' => $path,
'urls' => $urls,
'entity_info' => ['entity_type' => $entity_info['entity_type_name'], 'id' => $entity_id],
'lastmod' => method_exists($entity, 'getChangedTime') ? date_iso8601($entity->getChangedTime()) : NULL,
'priority' => isset($priority) ? $priority : (isset($entity_info['bundle_settings']['priority']) ? $entity_info['bundle_settings']['priority'] : NULL),
];
$priority = NULL;
$alternate_urls = [];
foreach ($languages as $language) {
$langcode = $language->getId();
if (!$batch_info['skip_untranslated'] || $language->isDefault() || $entity->hasTranslation($langcode)) {
$url_object->setOption('language', $language);
$alternate_urls[$langcode] = $url_object->toString();
}
}
foreach($alternate_urls as $langcode => $url) {
$context['results']['generate'][] = $path_data + ['langcode' => $langcode, 'url' => $url, 'alternate_urls' => $alternate_urls];
}
}
}
if (self::isBatch($batch_info)) {
self::setProgressInfo($context);
}
......@@ -263,29 +256,25 @@ class Batch {
? \Drupal::entityTypeManager()->getStorage(key($route_parameters))->load($route_parameters[key($route_parameters)])
: NULL;
$urls = [];
$path_data = [
'path' => $path,
'lastmod' => method_exists($entity, 'getChangedTime') ? date_iso8601($entity->getChangedTime()) : NULL,
'priority' => isset($custom_path['priority']) ? $custom_path['priority'] : NULL,
];
if (!is_null($entity)) {
$path_data['entity_info'] = ['entity_type' => $entity->getEntityTypeId(), 'id' => $entity->id()];
}
$alternate_urls = [];
foreach ($languages as $language) {
$langcode = $language->getId();
// Exclude untranslated paths.
if (!is_null($entity) && $batch_info['skip_untranslated']) {
if ($language->isDefault() || $entity->hasTranslation($langcode)) {
$url_object->setOption('language', $language);
$urls[$langcode] = $url_object->toString();
}
}
else {
if (!$batch_info['skip_untranslated'] || is_null($entity) || $entity->hasTranslation($langcode) || $language->isDefault()) {
$url_object->setOption('language', $language);
$urls[$langcode] = $url_object->toString();
$alternate_urls[$langcode] = $url_object->toString();
}
}
$context['results']['generate'][] = [
'path' => $path,
'urls' => $urls,
'lastmod' => method_exists($entity, 'getChangedTime') ? date_iso8601($entity->getChangedTime()) : NULL,
'priority' => isset($custom_path['priority']) ? $custom_path['priority'] : NULL,
];
foreach($alternate_urls as $langcode => $url) {
$context['results']['generate'][] = $path_data + ['langcode' => $langcode, 'url' => $url, 'alternate_urls' => $alternate_urls];
}
}
if (self::isBatch($batch_info)) {
self::setProgressInfo($context);
......
......@@ -171,20 +171,18 @@ class SitemapGenerator {
$writer->writeAttribute('xmlns:xhtml', self::XMLNS_XHTML);
foreach ($links as $link) {
$writer->startElement('url');
// Adding url to standard language.
$writer->writeElement('loc', $link['urls'][$this->defaultLanguageId]);
// Adding alternate urls (other languages) if any.
if (count($link['urls']) > 1) {
foreach($link['urls'] as $language_id => $localised_url) {
$writer->startElement('xhtml:link');
$writer->writeAttribute('rel', 'alternate');
$writer->writeAttribute('hreflang', $language_id);
$writer->writeAttribute('href', $localised_url);
$writer->endElement();
}
// Add each translation variant URL to the sitemap.
$writer->startElement('url');
$writer->writeElement('loc', $link['url']);
// Add all alternate URLs to this translation variant.
foreach($link['alternate_urls'] as $language_id => $alternate_url) {
$writer->startElement('xhtml:link');
$writer->writeAttribute('rel', 'alternate');
$writer->writeAttribute('hreflang', $language_id);
$writer->writeAttribute('href', $alternate_url);
$writer->endElement();
}
if (isset($link['priority'])) { // Add priority if any.
$writer->writeElement('priority', $link['priority']);
......
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