Commit 269ae9a9 authored by dermario's avatar dermario Committed by gbyte.co

Issue #3027057 by dermario, zenimagine, gbyte.co: Disable the hreflang method...

Issue #3027057 by dermario, zenimagine, gbyte.co: Disable the hreflang method of the language module
parent faf73438
......@@ -19,6 +19,9 @@ simple_sitemap.settings:
skip_untranslated:
label: 'Skip untranslated'
type: boolean
disable_language_hreflang:
label: 'Disable language hreflang'
type: boolean
xsl:
label: 'Include a stylesheet in the sitemaps for humans'
type: boolean
......
......@@ -230,3 +230,25 @@ function simple_sitemap_menu_delete(MenuInterface $menu) {
$generator = \Drupal::service('simple_sitemap.generator');
$generator->setVariants(TRUE)->removeBundleSettings('menu_link_content', $menu->id());
}
/**
* Implements hook_page_attachments_alter().
*/
function simple_sitemap_page_attachments_alter(array &$attachments) {
/** @var \Drupal\simple_sitemap\Simplesitemap $generator */
$generator = \Drupal::service('simple_sitemap.generator');
if ($generator->getSetting('disable_language_hreflang')) {
// @fixme https://www.drupal.org/project/drupal/issues/1255092
// Content Translation module normally adds identical hreflang tags, so
// executing its hook_page_attachments() implementation would be harmless,
// but if an entity page is configured as the front page, it attaches
// extraneous hreflang tags using the entity URL.
foreach ($attachments['#attached']['html_head_link'] as $key => $list) {
foreach ($list as $k => $element) {
if (!empty($element['hreflang']) && !empty($element['rel'])) {
unset($attachments['#attached']['html_head_link'][$key]);
}
}
}
}
}
......@@ -250,6 +250,13 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'#open' => FALSE,
];
$form['simple_sitemap_settings']['settings']['languages']['disable_language_hreflang'] = [
'#type' => 'checkbox',
'#title' => $this->t('Remove hreflang markup in HTML'),
'#description' => $this->t('Google recommends displaying hreflang definitions either in the HTML markup or in the sitemap, but not in both places.<br>If checked, hreflang definitions created by the language module will be removed from the markup reducing its size.'),
'#default_value' => $this->generator->getSetting('disable_language_hreflang', FALSE),
];
$form['simple_sitemap_settings']['settings']['languages']['skip_untranslated'] = [
'#type' => 'checkbox',
'#title' => $this->t('Skip non-existent translations'),
......@@ -384,7 +391,8 @@ class SimplesitemapSettingsForm extends SimplesitemapFormBase {
'skip_untranslated',
'xsl',
'base_url',
'default_variant'] as $setting_name) {
'default_variant',
'disable_language_hreflang'] as $setting_name) {
$this->generator->saveSetting($setting_name, $form_state->getValue($setting_name));
}
$this->generator->saveSetting('excluded_languages', array_filter($form_state->getValue('excluded_languages')));
......
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\simple_sitemap\Functional;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
......@@ -575,5 +576,22 @@ class SimplesitemapTest extends SimplesitemapTestBase {
$this->assertTrue($chunk_count > 1 ? (FALSE !== $index) : !$index);
}
/**
* Test the removal of hreflang tags in HTML.
*/
public function testHrefLangRemoval() {
// Test the nodes markup contains hreflang with default settings.
$this->generator->saveSetting('disable_language_hreflang', FALSE);
$this->drupalGet('node/' . $this->node->id());
$this->assertNotEmpty($this->xpath("//link[@hreflang]"));
Cache::invalidateTags($this->node->getCacheTags());
// Test the hreflang markup gets removed.
$this->generator->saveSetting('disable_language_hreflang', TRUE);
$this->drupalGet('node/' . $this->node->id());
$this->assertEmpty($this->xpath("//link[@hreflang]"));
}
}
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