diff --git a/src/Events/AfterSaveEvent.php b/src/Events/AfterSaveEvent.php index 5acb264..0d5196e 100644 --- a/src/Events/AfterSaveEvent.php +++ b/src/Events/AfterSaveEvent.php @@ -2,6 +2,9 @@ namespace Drupal\replicate\Events; +/** + * The AFTER_SAVE event occurs after a replicated entity was saved. + */ class AfterSaveEvent extends ReplicateEventBase { } diff --git a/src/Events/ReplicateAlterEvent.php b/src/Events/ReplicateAlterEvent.php index 220563c..884d2b1 100644 --- a/src/Events/ReplicateAlterEvent.php +++ b/src/Events/ReplicateAlterEvent.php @@ -4,20 +4,38 @@ namespace Drupal\replicate\Events; use Drupal\Core\Entity\EntityInterface; +/** + * The REPLICATE_ALTER event occurs when an entity is replicated but not saved. + */ class ReplicateAlterEvent extends ReplicateEventBase { /** + * The original entity. + * * @var \Drupal\Core\Entity\EntityInterface */ protected $original; - public function __construct(EntityInterface $entity, EntityInterface $original) { - parent::__construct($entity); + /** + * Constructs a new ReplicateAlterEvent. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The replicated entity. + * @param \Drupal\Core\Entity\EntityInterface $original + * The original entity. + * @param array $properties + * Arbitrary properties that may be used in Replicate events. + */ + public function __construct(EntityInterface $entity, EntityInterface $original, array $properties = []) { + parent::__construct($entity, $properties); $this->original = $original; } /** + * Returns the original entity. + * * @return \Drupal\Core\Entity\EntityInterface + * The original entity. */ public function getOriginal() { return $this->original; diff --git a/src/Events/ReplicateEntityEvent.php b/src/Events/ReplicateEntityEvent.php index d7eb53a..8f23697 100644 --- a/src/Events/ReplicateEntityEvent.php +++ b/src/Events/ReplicateEntityEvent.php @@ -2,6 +2,9 @@ namespace Drupal\replicate\Events; +/** + * This events occurs when an entity of a specific entity type is replicated. + */ class ReplicateEntityEvent extends ReplicateEventBase { } diff --git a/src/Events/ReplicateEntityFieldEvent.php b/src/Events/ReplicateEntityFieldEvent.php index 6b42383..e0ee0b6 100644 --- a/src/Events/ReplicateEntityFieldEvent.php +++ b/src/Events/ReplicateEntityFieldEvent.php @@ -5,6 +5,9 @@ namespace Drupal\replicate\Events; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Field\FieldItemListInterface; +/** + * This events occurs when a specific field type is replicated. + */ class ReplicateEntityFieldEvent extends ReplicateEventBase { /** @@ -21,14 +24,19 @@ class ReplicateEntityFieldEvent extends ReplicateEventBase { * The field item list. * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. + * @param array $properties + * Arbitrary properties that may be used in Replicate events. */ - public function __construct(FieldItemListInterface $field_item_list, EntityInterface $entity) { - parent::__construct($entity); + public function __construct(FieldItemListInterface $field_item_list, EntityInterface $entity, array $properties = []) { + parent::__construct($entity, $properties); $this->fieldItemList = $field_item_list; } /** - * @return FieldItemListInterface + * Returns the field item list. + * + * @return \Drupal\Core\Field\FieldItemListInterface + * The field item list. */ public function getFieldItemList() { return $this->fieldItemList; diff --git a/src/Events/ReplicateEventBase.php b/src/Events/ReplicateEventBase.php index 03d88ea..99b5ce5 100644 --- a/src/Events/ReplicateEventBase.php +++ b/src/Events/ReplicateEventBase.php @@ -5,22 +5,58 @@ namespace Drupal\replicate\Events; use Drupal\Component\EventDispatcher\Event; use Drupal\Core\Entity\EntityInterface; +/** + * A base class for events for the Replicate module. + * + * @package Drupal\replicate\Events + */ abstract class ReplicateEventBase extends Event { /** + * The replicated entity. + * * @var \Drupal\Core\Entity\EntityInterface */ protected $entity; - public function __construct(EntityInterface $entity) { + /** + * Arbitrary properties that may be used in Replicate events. + * + * @var array + */ + protected $properties; + + /** + * ReplicateEventBase constructor. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The replicated entity. + * @param array $properties + * Arbitrary properties that may be used in Replicate events. + */ + public function __construct(EntityInterface $entity, array $properties = []) { $this->entity = $entity; + $this->properties = $properties; } /** + * Returns the replicated entity. + * * @return \Drupal\Core\Entity\EntityInterface + * The replicated entity. */ public function getEntity() { return $this->entity; } + /** + * Returns the properties available for this event. + * + * @return array + * The available properties. + */ + public function getProperties() { + return $this->properties; + } + } diff --git a/src/Events/ReplicatorEvents.php b/src/Events/ReplicatorEvents.php index cb5209a..ed6dd05 100644 --- a/src/Events/ReplicatorEvents.php +++ b/src/Events/ReplicatorEvents.php @@ -2,27 +2,47 @@ namespace Drupal\replicate\Events; +/** + * Defines events for the Replicator module. + */ final class ReplicatorEvents { /** + * The AFTER_SAVE event occurs after a replicated entity was saved. + * * @see \Drupal\replicate\Events\AfterSaveEvent */ const AFTER_SAVE = 'replicate__after_save'; /** + * The REPLICATE_ALTER event is when the entity is replicated but not saved. + * * @see \Drupal\replicate\Events\ReplicateAlterEvent */ const REPLICATE_ALTER = 'replicate__alter'; /** - * @param $entity_type_id + * Returns an entity type specific event name. + * + * @param string $entity_type_id + * The entity type id. * * @return string + * The event name when an entity of this type is replicated. */ public static function replicateEntityEvent($entity_type_id) { return 'replicate__entity__' . $entity_type_id; } + /** + * Returns an entity field specific event name. + * + * @param string $field_type + * The field type. + * + * @return string + * The event name when an entity of this field type is replicated. + */ public static function replicateEntityField($field_type) { return 'replicate__entity_field__' . $field_type; } diff --git a/src/Replicator.php b/src/Replicator.php index bfe4648..46afe65 100644 --- a/src/Replicator.php +++ b/src/Replicator.php @@ -82,6 +82,8 @@ class Replicator { * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. + * @param array $properties + * Arbitrary properties that may be used in Replicate events. * * @return \Drupal\Core\Entity\EntityInterface|null * The cloned entity. @@ -93,8 +95,8 @@ class Replicator { * @throws \Drupal\Core\Entity\EntityStorageException * In case of failures, an exception is thrown. */ - public function replicateEntity(EntityInterface $entity) { - if ($clone = $this->cloneEntity($entity)) { + public function replicateEntity(EntityInterface $entity, array $properties = []) { + if ($clone = $this->cloneEntity($entity, $properties)) { $this->entityTypeManager->getStorage($entity->getEntityTypeId())->save($clone); $event = new AfterSaveEvent($clone); $this->eventDispatcher->dispatch($event, ReplicatorEvents::AFTER_SAVE); @@ -129,21 +131,22 @@ class Replicator { * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. + * @param array $properties + * Arbitrary properties that may be used in Replicate events. * * @return \Drupal\Core\Entity\EntityInterface|null * The cloned entity. */ - public function cloneEntity(EntityInterface $entity) { + public function cloneEntity(EntityInterface $entity, array $properties = []) { if ($clone = $entity->createDuplicate()) { - $event = new ReplicateEntityEvent($entity); + $event = new ReplicateEntityEvent($entity, $properties); $this->eventDispatcher->dispatch($event, ReplicatorEvents::replicateEntityEvent($entity->getEntityTypeId())); if ($clone instanceof FieldableEntityInterface) { - /** @var FieldableEntityInterface $clone */ - $this->dispatchEventCloneEntityFields($clone); + $this->dispatchEventCloneEntityFields($clone, $properties); } - $event = new ReplicateAlterEvent($clone, $entity); + $event = new ReplicateAlterEvent($clone, $entity, $properties); $this->eventDispatcher->dispatch($event, ReplicatorEvents::REPLICATE_ALTER); return $clone; } @@ -185,7 +188,6 @@ class Replicator { // @todo Can this ever happen? The interface only assures EntityInterface. throw new FieldException($this->t('Trying to clone into non fieldable Entity.')); } - /** @var FieldableEntityInterface $entity */ $violations = $target_field->validate(); if ($violations->count()) { @@ -202,10 +204,12 @@ class Replicator { * * @param \Drupal\Core\Entity\FieldableEntityInterface $clone * The cloned fieldable entity. + * @param array $properties + * Arbitrary properties that may be used in Replicate events. */ - protected function dispatchEventCloneEntityFields(FieldableEntityInterface $clone) { + protected function dispatchEventCloneEntityFields(FieldableEntityInterface $clone, array $properties = []) { foreach ($clone->getFieldDefinitions() as $field_name => $field_definition) { - $this->dispatchEventCloneEntityField($clone, $field_name, $field_definition); + $this->dispatchEventCloneEntityField($clone, $field_name, $field_definition, $properties); } } @@ -214,13 +218,15 @@ class Replicator { * * @param \Drupal\Core\Entity\FieldableEntityInterface $clone * The cloned fieldable entity. - * @param $field_name + * @param string $field_name * The field name. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition * The field definition. + * @param array $properties + * Arbitrary properties that may be used in Replicate events. */ - private function dispatchEventCloneEntityField(FieldableEntityInterface $clone, $field_name, FieldDefinitionInterface $field_definition) { - $event = new ReplicateEntityFieldEvent($clone->get($field_name), $clone); + private function dispatchEventCloneEntityField(FieldableEntityInterface $clone, $field_name, FieldDefinitionInterface $field_definition, array $properties = []) { + $event = new ReplicateEntityFieldEvent($clone->get($field_name), $clone, $properties); $this->eventDispatcher->dispatch($event, ReplicatorEvents::replicateEntityField($field_definition->getType())); }