Commit 163ac348 authored by JayKandari's avatar JayKandari Committed by Berdir
Browse files

Issue #2809447 by JayKandari, Berdir: new pattern types don't show after saving settings form

parent 39fa13d2
......@@ -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 }
<?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;
}
}
......@@ -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);
}
......
......@@ -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.
*
......
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