Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DDST-322: Dependency definitions #50

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions dgi_actions.post_update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* @file
* Post-update hooks.
*/

use Drupal\dgi_actions\Plugin\Action\IdentifierAction;

/**
* Resave `dgi_actions` entities to recalculate dependencies.
*/
function dgi_actions_post_update_ddst_322_dependencies() : void {
$entity_type_manager = \Drupal::entityTypeManager();

$to_resave = function () use ($entity_type_manager) {
yield from $entity_type_manager->getStorage('dgiactions_dataprofile')->loadMultiple();
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->getPlugin() 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();
}

}
36 changes: 28 additions & 8 deletions src/Entity/Identifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions src/Plugin/Action/IdentifierAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +19,8 @@
*/
abstract class IdentifierAction extends ConfigurableActionBase implements ContainerFactoryPluginInterface {

use DependencyTrait;

/**
* Identifier config.
*
Expand Down Expand Up @@ -204,4 +207,15 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
$this->configuration['identifier_entity'] = $form_state->getValue('identifier_entity');
}

/**
* {@inheritDoc}
*/
public function calculateDependencies() : array {
$this->addDependencies(parent::calculateDependencies());
if ($this->identifier) {
$this->addDependency($this->identifier->getConfigDependencyKey(), $this->identifier->getConfigDependencyName());
}
return $this->dependencies;
}

}
30 changes: 28 additions & 2 deletions src/Plugin/Condition/EntityHasIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +28,7 @@
*/
class EntityHasIdentifier extends ConditionPluginBase implements ContainerFactoryPluginInterface {

use DependencyTrait;
use StringTranslationTrait;

/**
Expand Down Expand Up @@ -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())) {
jordandukart marked this conversation as resolved.
Show resolved Hide resolved
$field = $identifier->get('field');
$entity_type = $identifier->get('entity');
$bundle = $identifier->get('bundle');
Expand All @@ -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}
*/
Expand Down Expand Up @@ -230,4 +244,16 @@ public function defaultConfiguration(): array {
);
}

/**
* {@inheritDoc}
*/
public function calculateDependencies() : array {
$this->addDependencies(parent::calculateDependencies());
if ($identifier = $this->getIdentifier()) {
$this->addDependency($identifier->getConfigDependencyKey(), $identifier->getConfigDependencyName());
}

return $this->dependencies;
}

}
Loading