Skip to content
Snippets Groups Projects
Commit c5fbc083 authored by christian.wiedemann's avatar christian.wiedemann Committed by Mikael Meulle
Browse files

Issue #3474141 by christian.wiedemann, just_like_good_vibes: New fields don't...

Issue #3474141 by christian.wiedemann, just_like_good_vibes: New fields don't appear in Data from a field until cache rebuild
parent 10e49833
Branches
Tags
1 merge request!211Add cache invalidation
Pipeline #282759 passed with warnings
......@@ -5,6 +5,8 @@ declare(strict_types=1);
namespace Drupal\ui_patterns;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\Context\ContextAwarePluginManagerInterface;
use Drupal\Core\Plugin\Context\ContextAwarePluginManagerTrait;
......@@ -26,6 +28,7 @@ class DerivableContextPluginManager extends DefaultPluginManager implements Cont
\Traversable $namespaces,
CacheBackendInterface $cache_backend,
ModuleHandlerInterface $module_handler,
protected EntityTypeManagerInterface $entityTypeManager,
) {
parent::__construct(
'Plugin/UiPatterns/DerivableContext',
......@@ -35,7 +38,7 @@ class DerivableContextPluginManager extends DefaultPluginManager implements Cont
DerivableContext::class
);
$this->alterInfo('derivable_context_info');
$this->setCacheBackend($cache_backend, 'ui_patterns_derivable_contexts_plugins');
$this->setCacheBackend($cache_backend, 'ui_patterns_derivable_contexts_plugins', $this->getInvalidationCacheTags());
}
/**
......@@ -58,4 +61,23 @@ class DerivableContextPluginManager extends DefaultPluginManager implements Cont
}
}
/**
* Get the cache tags to invalidate.
*/
protected function getInvalidationCacheTags() : array {
$tags = [];
$entity_type_definitions = $this->entityTypeManager->getDefinitions();
foreach ($entity_type_definitions as $entity_type_definition) {
if (!$entity_type_definition->entityClassImplements(FieldableEntityInterface::class)) {
// Skip entity not using fields.
continue;
}
$bundle_entity_type = $entity_type_definition->getBundleEntityType();
if ($bundle_entity_type) {
$tags[] = sprintf("config:%s_list", $bundle_entity_type);
}
}
return $tags;
}
}
......@@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityTypeEventSubscriberTrait;
use Drupal\Core\Entity\EntityTypeListenerInterface;
use Drupal\Core\Field\FieldStorageDefinitionEvent;
use Drupal\Core\Field\FieldStorageDefinitionEventSubscriberTrait;
use Drupal\ui_patterns\DerivableContextPluginManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
......@@ -26,8 +27,10 @@ class UiPatternsEntitySchemaSubscriber implements EntityTypeListenerInterface, E
*
* @param \Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface $sourceManager
* The entity type manager.
* @param \Drupal\ui_patterns\DerivableContextPluginManager $derivableContextPluginManager
* The derivable context plugin manager.
*/
public function __construct(protected CachedDiscoveryInterface $sourceManager) {
public function __construct(protected CachedDiscoveryInterface $sourceManager, protected DerivableContextPluginManager $derivableContextPluginManager) {
}
/**
......@@ -36,7 +39,7 @@ class UiPatternsEntitySchemaSubscriber implements EntityTypeListenerInterface, E
public static function getSubscribedEvents(): array {
return array_merge(
static::getEntityTypeEvents(),
static::getFieldStorageDefinitionEvents()
static::getFieldStorageDefinitionEvents(),
);
}
......@@ -45,6 +48,7 @@ class UiPatternsEntitySchemaSubscriber implements EntityTypeListenerInterface, E
*/
public function onEntityTypeEvent(EntityTypeEvent $event, string $event_name): void {
$this->sourceManager->clearCachedDefinitions();
$this->derivableContextPluginManager->clearCachedDefinitions();
}
/**
......@@ -52,6 +56,7 @@ class UiPatternsEntitySchemaSubscriber implements EntityTypeListenerInterface, E
*/
public function onFieldStorageDefinitionEvent(FieldStorageDefinitionEvent $event, string $event_name): void {
$this->sourceManager->clearCachedDefinitions();
$this->derivableContextPluginManager->clearCachedDefinitions();
}
}
......@@ -7,6 +7,7 @@
declare(strict_types=1);
use Drupal\Core\Field\FieldConfigInterface;
use Drupal\ui_patterns\Plugin\UiPatterns\Source\WysiwygWidget;
/**
......@@ -81,3 +82,16 @@ function ui_patterns_plugin_filter_block__ui_patterns_alter(array &$definitions,
$definitions[$id]['_ui_patterns_compatible'] = $compatibilityFlag;
}
}
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function ui_patterns_field_config_delete(FieldConfigInterface $field_config): void {
$sample_entity_generator = \Drupal::service('ui_patterns.sample_entity_generator');
$entity_type = $field_config->getTargetEntityTypeId();
$bundle = $field_config->getTargetBundle();
$sample_entity_generator->delete($entity_type, $bundle);
// @todo trigger an event and do this logic in UiPatternsEntitySchemaSubscriber
\Drupal::service('plugin.manager.ui_patterns_source')->clearCachedDefinitions();
\Drupal::service('plugin.manager.ui_patterns_derivable_context')->clearCachedDefinitions();
}
......@@ -37,7 +37,8 @@ services:
plugin.manager.ui_patterns_derivable_context:
class: Drupal\ui_patterns\DerivableContextPluginManager
parent: default_plugin_manager
arguments:
- "@entity_type.manager"
# Render elements management.
ui_patterns.component_element_builder:
class: Drupal\ui_patterns\Element\ComponentElementBuilder
......@@ -81,7 +82,7 @@ services:
ui_patterns.entity_schema_subscriber:
class: Drupal\ui_patterns\EventSubscriber\UiPatternsEntitySchemaSubscriber
arguments: ["@plugin.manager.ui_patterns_source"]
arguments: ["@plugin.manager.ui_patterns_source", "@plugin.manager.ui_patterns_derivable_context"]
tags:
- { name: "event_subscriber" }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment