Skip to content

Commit

Permalink
Add more dependencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-vessey committed Jul 3, 2024
1 parent 5c4250d commit 4f002a8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
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())) {
$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,
);
}

}

0 comments on commit 4f002a8

Please sign in to comment.