From 4f002a8f8963e67650c0e9777ff49263c16d404c Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 3 Jul 2024 11:52:04 -0300 Subject: [PATCH] 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, + ); + } + }