diff --git a/src/EntityClone/Content/ContentEntityCloneBase.php b/src/EntityClone/Content/ContentEntityCloneBase.php
index 8b621aa..cc8c25e 100644
--- a/src/EntityClone/Content/ContentEntityCloneBase.php
+++ b/src/EntityClone/Content/ContentEntityCloneBase.php
@@ -6,8 +6,11 @@ use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManager;
+use Drupal\entity_clone\Events\EntityCloneEvent;
+use Drupal\entity_clone\Events\EntityCloneEvents;
use Drupal\entity_clone\EntityClone\EntityCloneInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Class ContentEntityCloneBase.
@@ -29,16 +32,26 @@ class ContentEntityCloneBase implements EntityHandlerInterface, EntityCloneInter
protected $entityTypeId;
/**
+ * Event dispatcher service.
+ *
+ * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
+ */
+ protected $eventDispatcher;
+
+ /**
* Constructs a new ContentEntityCloneBase.
*
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
* The entity type manager.
* @param string $entity_type_id
* The entity type ID.
+ * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
+ * The event dispatcher service.
*/
- public function __construct(EntityTypeManager $entity_type_manager, $entity_type_id) {
+ public function __construct(EntityTypeManager $entity_type_manager, $entity_type_id, EventDispatcherInterface $eventDispatcher) {
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeId = $entity_type_id;
+ $this->eventDispatcher = $eventDispatcher;
}
/**
@@ -47,7 +60,8 @@ class ContentEntityCloneBase implements EntityHandlerInterface, EntityCloneInter
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$container->get('entity_type.manager'),
- $entity_type->id()
+ $entity_type->id(),
+ $container->get('event_dispatcher')
);
}
@@ -59,7 +73,11 @@ class ContentEntityCloneBase implements EntityHandlerInterface, EntityCloneInter
if ($label_key = $this->entityTypeManager->getDefinition($this->entityTypeId)->getKey('label')) {
$cloned_entity->set($label_key, $entity->label() . ' - Cloned');
}
+
+ $this->eventDispatcher->dispatch(EntityCloneEvents::PRE_SAVE, new EntityCloneEvent($entity, $cloned_entity, $properties));
$cloned_entity->save();
+ $this->eventDispatcher->dispatch(EntityCloneEvents::POST_SAVE, new EntityCloneEvent($entity, $cloned_entity, $properties));
+
return $cloned_entity;
}
diff --git a/src/Events/EntityCloneEvent.php b/src/Events/EntityCloneEvent.php
new file mode 100644
index 0000000..45cd1ce
--- /dev/null
+++ b/src/Events/EntityCloneEvent.php
@@ -0,0 +1,74 @@
+entity = $entity;
+ $this->cloned_entity = $cloned_entity;
+ $this->properties = $properties;
+ }
+
+ /**
+ * Gets entity being cloned.
+ *
+ * @return \Drupal\Core\Entity\EntityInterface
+ */
+ public function getEntity() {
+ return $this->entity;
+ }
+
+ /**
+ * Gets new cloned entity.
+ *
+ * @return \Drupal\Core\Entity\EntityInterface
+ */
+ public function getClonedEntity() {
+ return $this->cloned_entity;
+ }
+
+ /**
+ * Gets entity properties.
+ *
+ * @return array
+ */
+ public function getProperties() {
+ return $this->properties;
+ }
+
+}
diff --git a/src/Events/EntityCloneEvents.php b/src/Events/EntityCloneEvents.php
new file mode 100644
index 0000000..c6b8f5d
--- /dev/null
+++ b/src/Events/EntityCloneEvents.php
@@ -0,0 +1,42 @@
+entityTypeManager = $entity_type_manager;
$this->stringTranslationManager = $string_translation;
+ $this->eventDispatcher = $eventDispatcher;
$parameter_name = $route_match->getRouteObject()->getOption('_entity_clone_entity_type_id');
$this->entity = $route_match->getParameter($parameter_name);
@@ -73,7 +86,8 @@ class EntityCloneForm extends FormBase {
return new static(
$container->get('entity_type.manager'),
$container->get('current_route_match'),
- $container->get('string_translation')
+ $container->get('string_translation'),
+ $container->get('event_dispatcher')
);
}
@@ -137,7 +151,11 @@ class EntityCloneForm extends FormBase {
$properties = $entity_clone_form_handler->getNewValues($form_state);
}
- $cloned_entity = $entity_clone_handler->cloneEntity($this->entity, $this->entity->createDuplicate(), $properties);
+ $duplicate = $this->entity->createDuplicate();
+
+ $this->eventDispatcher->dispatch(EntityCloneEvents::PRE_CLONE, new EntityCloneEvent($this->entity, $duplicate, $properties));
+ $cloned_entity = $entity_clone_handler->cloneEntity($this->entity, $duplicate, $properties);
+ $this->eventDispatcher->dispatch(EntityCloneEvents::POST_CLONE, new EntityCloneEvent($this->entity, $duplicate, $properties));
drupal_set_message($this->stringTranslationManager->translate('The entity @entity (@entity_id) of type @type was cloned', [
'@entity' => $this->entity->label(),