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 1 commit
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
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
16 changes: 16 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,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,
);
}

}
32 changes: 30 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,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,
);
}

}
Loading