Unverified Commit 524395fc authored by Dave Reid's avatar Dave Reid Committed by Dave Reid

Issue #3092690 by Dave Reid: Added setting to exclude items that have...

Issue #3092690 by Dave Reid: Added setting to exclude items that have individually overridden the robots meta tag to noindex.
parent b11aeaa8
......@@ -11,3 +11,4 @@ gz: false
clean_url: 0
disable_cron_regeneration: false
i18n_selection_mode: 'simple'
metatag_exclude_noindex: true
......@@ -43,6 +43,9 @@ xmlsitemap.settings:
i18n_selection_mode:
type: string
label: 'Language selection mode'
metatag_exclude_noindex:
type: boolean
label: 'Exclude individual content that has the Robots meta tag with <em>Prevents search engines from indexing this page</em>.'
xmlsitemap.settings.*.*:
type: config_object
......
......@@ -2,6 +2,7 @@
namespace Drupal\xmlsitemap\Form;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Render\Element;
......@@ -39,6 +40,13 @@ class XmlSitemapSettingsForm extends ConfigFormBase {
*/
protected $linkStorage;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a new XmlSitemapSettingsForm object.
*
......@@ -50,13 +58,16 @@ class XmlSitemapSettingsForm extends ConfigFormBase {
* The date formatter service.
* @param \Drupal\xmlsitemap\XmlSitemapLinkStorageInterface $link_storage
* The xmlsitemap link storage service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(ConfigFactoryInterface $config_factory, StateInterface $state, DateFormatterInterface $date, XmlSitemapLinkStorageInterface $link_storage) {
public function __construct(ConfigFactoryInterface $config_factory, StateInterface $state, DateFormatterInterface $date, XmlSitemapLinkStorageInterface $link_storage, ModuleHandlerInterface $module_handler) {
parent::__construct($config_factory);
$this->state = $state;
$this->date = $date;
$this->linkStorage = $link_storage;
$this->moduleHandler = $module_handler;
}
/**
......@@ -64,7 +75,11 @@ class XmlSitemapSettingsForm extends ConfigFormBase {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'), $container->get('state'), $container->get('date.formatter'), $container->get('xmlsitemap.link_storage')
$container->get('config.factory'),
$container->get('state'),
$container->get('date.formatter'),
$container->get('xmlsitemap.link_storage'),
$container->get('module_handler')
);
}
......@@ -125,6 +140,13 @@ class XmlSitemapSettingsForm extends ConfigFormBase {
'#default_value' => $config->get('prefetch_aliases'),
'#access' => FALSE,
];
$form['metatag_exclude_noindex'] = [
'#type' => 'checkbox',
'#title' => $this->t('Exclude individual items that has the Robots meta tag set to <em>Prevents search engines from indexing this page</em>.'),
'#description' => $this->t('Note this will ignore default metatags, only when items have overridden the Robots meta tag.'),
'#default_value' => $config->get('metatag_exclude_noindex'),
'#access' => $this->moduleHandler->moduleExists('metatag'),
];
$form['advanced'] = [
'#type' => 'details',
......
......@@ -9,17 +9,6 @@ namespace Drupal\Tests\xmlsitemap\Kernel;
*/
class DirectoryTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
// This is required to not fail the @covers for global functions.
// @todo Once xmlsitemap_clear_directory() is refactored to auto-loadable code, remove this require statement.
require_once __DIR__ . '/../../../xmlsitemap.module';
}
/**
* Test xmlsitemap_clear_directory().
*
......
......@@ -17,6 +17,17 @@ abstract class KernelTestBase extends CoreKernelTestBase {
'xmlsitemap',
];
/**
* {@inheritdoc}
*/
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
// This is required to not fail the @covers for global functions.
// @todo Once xmlsitemap_clear_directory() is refactored to auto-loadable code, remove this require statement.
require_once __DIR__ . '/../../../xmlsitemap.module';
}
/**
* {@inheritdoc}
*/
......
<?php
namespace Drupal\Tests\xmlsitemap\Kernel;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\Entity\Role;
/**
* Tests integration with the Metatag module.
*
* @group xmlsitemap
*
* @covers ::metatag_xmlsitemap_link_alter
*/
class MetatagNoIndexTest extends KernelTestBase {
use UserCreationTrait;
/**
* The xmlsitemap link storage handler.
*
* @var \Drupal\xmlsitemap\XmlSitemapLinkStorageInterface
*/
protected $linkStorage;
/**
* The account object.
*
* @var \Drupal\user\UserInterface
*/
protected $account;
/**
* {@inheritdoc}
*/
public static $modules = [
'field',
'user',
'token',
'metatag',
];
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->installConfig(['system', 'user', 'field', 'metatag']);
$this->installEntitySchema('user');
$this->installSchema('system', ['sequences']);
// Allow anonymous user to view user profiles.
$role = Role::load(AccountInterface::ANONYMOUS_ROLE);
$role->grantPermission('access user profiles');
$role->save();
// Enable XML Sitemap settings for users.
xmlsitemap_link_bundle_enable('user', 'user');
xmlsitemap_link_bundle_settings_save('user', 'user', [
'status' => 1,
'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
]);
// Create a generic metatag field.
FieldStorageConfig::create([
'entity_type' => 'user',
'field_name' => 'field_metatags',
'type' => 'metatag',
])->save();
FieldConfig::create([
'entity_type' => 'user',
'field_name' => 'field_metatags',
'bundle' => 'user',
])->save();
$this->linkStorage = $this->container->get('xmlsitemap.link_storage');
$this->account = $this->createUser();
// Test that the user is visible in the sitemap by default.
$link = $this->linkStorage->load('user', $this->account->id());
$this->assertTrue($link['access'] && $link['status']);
}
/**
* Tests overriding an entity's robots meta tag.
*/
public function testEntityNoIndex() {
// Set the robots metatago on the user entity.
$this->account->set('field_metatags', serialize(['robots' => 'noindex']));
$this->account->save();
// Test that the user is not visible in the sitemap now.
$link = $this->linkStorage->load('user', $this->account->id());
$this->assertFalse($link['access'] && $link['status']);
// Disable the metatag noindex configuration.
$this->config('xmlsitemap.settings')->set('metatag_exclude_noindex', FALSE)->save(TRUE);
drupal_static_reset('metatag_xmlsitemap_link_alter');
// Test that the user is visible in the sitemap again.
$this->account->save();
$link = $this->linkStorage->load('user', $this->account->id());
$this->assertTrue($link['access'] && $link['status']);
}
/**
* Tests that default robots metatags are ignored
*/
public function testDefaultsNoIndex() {
// Set the user entity default robots metatag to noindex.
$config = $this->config('metatag.metatag_defaults.user');
$config->set('tags.robots', 'noindex');
$config->save();
// Test that this hasn't changed the link availability.
$this->account->save();
$link = $this->linkStorage->load('user', $this->account->id());
$this->assertTrue($link['access'] && $link['status']);
}
}
......@@ -2714,3 +2714,22 @@ function xmlsitemap_link_entity_check_enabled($entity_type_id) {
$configuration = \Drupal::configFactory()->listAll("xmlsitemap.settings.{$entity_type_id}.");
return !empty($configuration);
}
/**
* Implements hook_xmlsitemap_link_alter() on behalf of metatag.
*/
function metatag_xmlsitemap_link_alter(array &$link, array $context) {
$enabled = &drupal_static(__FUNCTION__);
if (!isset($enabled)) {
$enabled = \Drupal::config('xmlsitemap.settings')->get('metatag_exclude_noindex');
}
if ($enabled && !empty($context['entity']) && $context['entity'] instanceof ContentEntityInterface && $link['access']) {
/** @var \Drupal\metatag\MetatagManagerInterface $metatagManager */
$metatagManager = \Drupal::service('metatag.manager');
$metatags = $metatagManager->tagsFromEntity($context['entity']);
if (!empty($metatags['robots']) && strpos($metatags['robots'], 'noindex') !== FALSE) {
$link['access'] = FALSE;
}
}
}
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