From 4f002a8f8963e67650c0e9777ff49263c16d404c Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 3 Jul 2024 11:52:04 -0300 Subject: [PATCH 1/4] Add more dependencies. --- src/Entity/Identifier.php | 36 +++++++++++++++----- src/Plugin/Action/IdentifierAction.php | 16 +++++++++ src/Plugin/Condition/EntityHasIdentifier.php | 32 +++++++++++++++-- 3 files changed, 74 insertions(+), 10 deletions(-) diff --git a/src/Entity/Identifier.php b/src/Entity/Identifier.php index 4e1b4b1..34d078f 100644 --- a/src/Entity/Identifier.php +++ b/src/Entity/Identifier.php @@ -3,6 +3,8 @@ namespace Drupal\dgi_actions\Entity; use Drupal\Core\Config\Entity\ConfigEntityBase; +use Drupal\Core\Field\FieldConfigInterface; +use Drupal\Core\Field\FieldDefinitionInterface; /** * Defines the Identifier setting entity. @@ -122,14 +124,30 @@ public function getField(): string { * {@inheritdoc} */ public function getServiceData(): ?ServiceDataInterface { - return \Drupal::service('entity_type.manager')->getStorage('dgiactions_servicedata')->load($this->service_data); + return $this->service_data ? + \Drupal::service('entity_type.manager')->getStorage('dgiactions_servicedata')->load($this->service_data) : + NULL; } /** * {@inheritdoc} */ public function getDataProfile(): ?DataProfileInterface { - return \Drupal::service('entity_type.manager')->getStorage('dgiactions_dataprofile')->load($this->data_profile); + return $this->data_profile ? + \Drupal::service('entity_type.manager')->getStorage('dgiactions_dataprofile')->load($this->data_profile) : + NULL; + } + + /** + * Helper; get the entity representing the field of the entity. + * + * @return \Drupal\Core\Field\FieldDefinitionInterface|null + * The entity representing the field if it could be found; otherwise, NULL. + */ + protected function getFieldEntity() : ?FieldDefinitionInterface { + /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */ + $entity_field_manager = \Drupal::service('entity_field.manager'); + return $entity_field_manager->getFieldDefinitions($this->getEntity(), $this->getBundle())[$this->getField()] ?? NULL; } /** @@ -140,14 +158,16 @@ public function calculateDependencies() { // Add the dependency on the data profile and service data entities if // they are here. - if ($this->data_profile) { - $profile_entity = $this->getDataProfile(); - $this->addDependency('config', $profile_entity->getConfigDependencyName()); + if ($profile_entity = $this->getDataProfile()) { + $this->addDependency($profile_entity->getConfigDependencyKey(), $profile_entity->getConfigDependencyName()); + } + + if ($service_entity = $this->getServiceData()) { + $this->addDependency($service_entity->getConfigDependencyKey(), $service_entity->getConfigDependencyName()); } - if ($this->service_data) { - $service_entity = $this->getServiceData(); - $this->addDependency('config', $service_entity->getConfigDependencyName()); + if (($field_entity = $this->getFieldEntity()) && $field_entity instanceof FieldConfigInterface) { + $this->addDependency($field_entity->getConfigDependencyKey(), $field_entity->getConfigDependencyName()); } return $this; diff --git a/src/Plugin/Action/IdentifierAction.php b/src/Plugin/Action/IdentifierAction.php index 05ffb13..ab4efb1 100644 --- a/src/Plugin/Action/IdentifierAction.php +++ b/src/Plugin/Action/IdentifierAction.php @@ -3,6 +3,7 @@ namespace Drupal\dgi_actions\Plugin\Action; use Drupal\Core\Action\ConfigurableActionBase; +use Drupal\Core\Entity\DependencyTrait; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; @@ -18,6 +19,8 @@ */ abstract class IdentifierAction extends ConfigurableActionBase implements ContainerFactoryPluginInterface { + use DependencyTrait; + /** * Identifier config. * @@ -204,4 +207,17 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s $this->configuration['identifier_entity'] = $form_state->getValue('identifier_entity'); } + /** + * {@inheritDoc} + */ + public function calculateDependencies() : array { + if ($this->identifier) { + $this->addDependency($this->identifier->getConfigDependencyKey(), $this->identifier->getConfigDependencyName()); + } + return array_merge_recursive( + parent::calculateDependencies(), + $this->dependencies, + ); + } + } diff --git a/src/Plugin/Condition/EntityHasIdentifier.php b/src/Plugin/Condition/EntityHasIdentifier.php index 8652220..bbf32ff 100644 --- a/src/Plugin/Condition/EntityHasIdentifier.php +++ b/src/Plugin/Condition/EntityHasIdentifier.php @@ -4,11 +4,13 @@ use Drupal\Component\Render\MarkupInterface; use Drupal\Core\Condition\ConditionPluginBase; +use Drupal\Core\Entity\DependencyTrait; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\dgi_actions\Entity\IdentifierInterface; use Drupal\dgi_actions\Utility\IdentifierUtils; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -26,6 +28,7 @@ */ class EntityHasIdentifier extends ConditionPluginBase implements ContainerFactoryPluginInterface { + use DependencyTrait; use StringTranslationTrait; /** @@ -108,8 +111,7 @@ public function evaluate(): bool { } $entity = $this->getContextValue('entity'); - if ($entity instanceof FieldableEntityInterface) { - $identifier = $this->entityTypeManager->getStorage('dgiactions_identifier')->load($this->configuration['identifier']); + if ($entity instanceof FieldableEntityInterface && ($identifier = $this->getIdentifier())) { $field = $identifier->get('field'); $entity_type = $identifier->get('entity'); $bundle = $identifier->get('bundle'); @@ -120,6 +122,18 @@ public function evaluate(): bool { return FALSE; } + /** + * Helper; get the target identifier entity. + * + * @return \Drupal\dgi_actions\Entity\IdentifierInterface|null + * The loaded entity, or NULL. + */ + protected function getIdentifier() : ?IdentifierInterface { + return $this->configuration['identifier'] ? + $this->entityTypeManager->getStorage('dgiactions_identifier')->load($this->configuration['identifier']) : + NULL; + } + /** * {@inheritdoc} */ @@ -230,4 +244,18 @@ public function defaultConfiguration(): array { ); } + /** + * {@inheritDoc} + */ + public function calculateDependencies() { + if ($identifier = $this->getIdentifier()) { + $this->addDependency($identifier->getConfigDependencyKey(), $identifier->getConfigDependencyName()); + } + + return array_merge_recursive( + parent::calculateDependencies(), + $this->dependencies, + ); + } + } From 0f95fce063f32f1cf1bdbc130f027ebefb6bfaf2 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 3 Jul 2024 17:06:26 -0300 Subject: [PATCH 2/4] Try rolling post-update hook to update deps. --- dgi_actions.post_update.php | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 dgi_actions.post_update.php diff --git a/dgi_actions.post_update.php b/dgi_actions.post_update.php new file mode 100644 index 0000000..d914f05 --- /dev/null +++ b/dgi_actions.post_update.php @@ -0,0 +1,47 @@ +getStorage('dgiactions_dataprofile')->loadMultiple(); + yield from $entity_type_manager->getStorage('dgiactions_servicedata')->loadMultiple(); + yield from $entity_type_manager->getStorage('dgiactions_identifier')->loadMultiple(); + + foreach ($entity_type_manager->getStorage('action')->loadByProperties(['type' => 'entity']) as $id => $action) { + if ($action instanceof IdentifierAction) { + yield $id => $action; + } + } + + /** + * @var string $id + * @var \Drupal\context\ContextInterface $context + */ + foreach ($entity_type_manager->getStorage('context')->loadMultiple() as $id => $context) { + if ( + $context->hasCondition('dgi_actions_entity_persistent_identifier_populated') || + $context->hasReaction('dgi_actions_entity_mint_reaction') || + $context->hasReaction('dgi_actions_entity_delete_reaction') + ) { + yield $id => $context; + } + } + }; + + /** @var \Drupal\Core\Entity\EntityInterface $config_entity */ + foreach ($to_resave() as $config_entity) { + $config_entity->save(); + } + +} From 2b6d0f7841ae30c87dfa153ce826efa63feabf95 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 3 Jul 2024 17:29:56 -0300 Subject: [PATCH 3/4] Fix up plugin detection. --- dgi_actions.post_update.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dgi_actions.post_update.php b/dgi_actions.post_update.php index d914f05..b23701e 100644 --- a/dgi_actions.post_update.php +++ b/dgi_actions.post_update.php @@ -18,8 +18,11 @@ function dgi_actions_post_update_ddst_322_dependencies() : void { yield from $entity_type_manager->getStorage('dgiactions_servicedata')->loadMultiple(); yield from $entity_type_manager->getStorage('dgiactions_identifier')->loadMultiple(); + /** + * @var \Drupal\system\ActionConfigEntityInterface $action + */ foreach ($entity_type_manager->getStorage('action')->loadByProperties(['type' => 'entity']) as $id => $action) { - if ($action instanceof IdentifierAction) { + if ($action->getPlugin() instanceof IdentifierAction) { yield $id => $action; } } From 13905fba51a6a429754f4baba41abc0d98fc67a1 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 3 Jul 2024 18:05:26 -0300 Subject: [PATCH 4/4] Ensure ordering of dependencies is consistent. Maybe paranoia, but the use of `array_merge_recursive()` would have allowed the order to be inconsistent, potentially resulting it future noise in imports/exports with stuff randomly moving around. Having the order consistent should prevent such from happening. --- src/Plugin/Action/IdentifierAction.php | 6 ++---- src/Plugin/Condition/EntityHasIdentifier.php | 8 +++----- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Plugin/Action/IdentifierAction.php b/src/Plugin/Action/IdentifierAction.php index ab4efb1..8049a8a 100644 --- a/src/Plugin/Action/IdentifierAction.php +++ b/src/Plugin/Action/IdentifierAction.php @@ -211,13 +211,11 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s * {@inheritDoc} */ public function calculateDependencies() : array { + $this->addDependencies(parent::calculateDependencies()); if ($this->identifier) { $this->addDependency($this->identifier->getConfigDependencyKey(), $this->identifier->getConfigDependencyName()); } - return array_merge_recursive( - parent::calculateDependencies(), - $this->dependencies, - ); + return $this->dependencies; } } diff --git a/src/Plugin/Condition/EntityHasIdentifier.php b/src/Plugin/Condition/EntityHasIdentifier.php index bbf32ff..0ade798 100644 --- a/src/Plugin/Condition/EntityHasIdentifier.php +++ b/src/Plugin/Condition/EntityHasIdentifier.php @@ -247,15 +247,13 @@ public function defaultConfiguration(): array { /** * {@inheritDoc} */ - public function calculateDependencies() { + public function calculateDependencies() : array { + $this->addDependencies(parent::calculateDependencies()); if ($identifier = $this->getIdentifier()) { $this->addDependency($identifier->getConfigDependencyKey(), $identifier->getConfigDependencyName()); } - return array_merge_recursive( - parent::calculateDependencies(), - $this->dependencies, - ); + return $this->dependencies; } }