From 163ac3485a6ec718a03342514d2de992b1d9672f Mon Sep 17 00:00:00 2001
From: JayKandari <JayKandari@2876519.no-reply.drupal.org>
Date: Thu, 19 Jan 2017 00:09:31 +0100
Subject: [PATCH] Issue #2809447 by JayKandari, Berdir: new pattern types don't
show after saving settings form
---
pathauto.services.yml | 5 ++
.../PathautoSettingsCacheTag.php | 54 +++++++++++++++++++
src/Form/PathautoSettingsForm.php | 6 ---
tests/src/Kernel/PathautoKernelTest.php | 27 ++++++++++
4 files changed, 86 insertions(+), 6 deletions(-)
create mode 100644 src/EventSubscriber/PathautoSettingsCacheTag.php
diff --git a/pathauto.services.yml b/pathauto.services.yml
index 1060e48c..3ce704ec 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 00000000..371fe2e2
--- /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 c17ac99a..b4e85b49 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 c3438b5c..e4d7df6e 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.
*
--
GitLab