diff --git a/pathauto.services.yml b/pathauto.services.yml index 1060e48c4325d01d68d4ffeb3d5fe5e3257790d4..3ce704ec7f0c30e924f6e1d1785d08997f18c967 100644 --- a/pathauto.services.yml +++ b/pathauto.services.yml @@ -19,3 +19,8 @@ services: plugin.manager.alias_type: class: Drupal\pathauto\AliasTypeManager parent: default_plugin_manager + pathauto.settings_cache_tag: + class: Drupal\pathauto\EventSubscriber\PathautoSettingsCacheTag + arguments: ['@entity_field.manager', '@plugin.manager.alias_type'] + tags: + - { name: event_subscriber } diff --git a/src/EventSubscriber/PathautoSettingsCacheTag.php b/src/EventSubscriber/PathautoSettingsCacheTag.php new file mode 100644 index 0000000000000000000000000000000000000000..371fe2e2fc8b7367d377cdcf9492fb7ce22f00bd --- /dev/null +++ b/src/EventSubscriber/PathautoSettingsCacheTag.php @@ -0,0 +1,54 @@ +<?php + +namespace Drupal\pathauto\EventSubscriber; + +use Drupal\Core\Config\ConfigCrudEvent; +use Drupal\Core\Config\ConfigEvents; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Drupal\Core\Entity\EntityFieldManagerInterface; +use Drupal\pathauto\AliasTypeManager; + +/** + * A subscriber to clear fielddefinition cache when saving pathauto settings. + */ +class PathautoSettingsCacheTag implements EventSubscriberInterface { + + protected $entityFieldManager; + protected $aliasTypeManager; + + /** + * Constructs a PathautoSettingsCacheTag object. + */ + public function __construct(EntityFieldManagerInterface $entity_field_manager, AliasTypeManager $alias_type_manager) { + $this->entityFieldManager = $entity_field_manager; + $this->aliasTypeManager = $alias_type_manager; + } + + /** + * Invalidate the 'rendered' cache tag whenever the settings are modified. + * + * @param \Drupal\Core\Config\ConfigCrudEvent $event + * The Event to process. + */ + public function onSave(ConfigCrudEvent $event) { + if ($event->getConfig()->getName() === 'pathauto.settings') { + $config = $event->getConfig(); + $original_entity_types = $config->getOriginal('enabled_entity_types'); + + // Clear cached field definitions if the values are changed. + if ($original_entity_types != $config->get('enabled_entity_types')) { + $this->entityFieldManager->clearCachedFieldDefinitions(); + $this->aliasTypeManager->clearCachedDefinitions(); + } + } + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events[ConfigEvents::SAVE][] = ['onSave']; + return $events; + } + +} diff --git a/src/Form/PathautoSettingsForm.php b/src/Form/PathautoSettingsForm.php index c17ac99a887b286d524b6d390aafdcae339092e1..b4e85b49558663778d91b241c15faf1904e5ba72 100644 --- a/src/Form/PathautoSettingsForm.php +++ b/src/Form/PathautoSettingsForm.php @@ -260,12 +260,6 @@ class PathautoSettingsForm extends ConfigFormBase { } $config->save(); - // Clear cached field definitions if the values are changed. - if ($original_entity_types != $config->get('enabled_entity_types')) { - $this->entityFieldManager->clearCachedFieldDefinitions(); - $this->aliasTypeManager->clearCachedDefinitions(); - } - parent::submitForm($form, $form_state); } diff --git a/tests/src/Kernel/PathautoKernelTest.php b/tests/src/Kernel/PathautoKernelTest.php index c3438b5c0e7f22171de8c60c2ba374d8f5d416a6..e4d7df6efcfca9dba6652bb23d6b0513fc2f5e25 100644 --- a/tests/src/Kernel/PathautoKernelTest.php +++ b/tests/src/Kernel/PathautoKernelTest.php @@ -505,6 +505,33 @@ class PathautoKernelTest extends KernelTestBase { $this->assertNoEntityAlias($node2); } + /** + * Tests that enabled entity types genrates the necessary fields and plugins. + */ + public function testSettingChangeInvalidatesCache() { + + $this->installConfig(['pathauto']); + + $this->enableModules(['entity_test']); + + $definitions = \Drupal::service('plugin.manager.alias_type')->getDefinitions(); + $this->assertFalse(isset($definitions['canonical_entities:entity_test'])); + + $fields = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions('entity_test'); + $this->assertFalse(isset($fields['path'])); + + $this->config('pathauto.settings') + ->set('enabled_entity_types', ['user', 'entity_test']) + ->save(); + + $definitions = \Drupal::service('plugin.manager.alias_type')->getDefinitions(); + $this->assertTrue(isset($definitions['canonical_entities:entity_test'])); + + $fields = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions('entity_test'); + $this->assertTrue(isset($fields['path'])); + + } + /** * Creates a node programmatically. *