Add PRE_CLONE/POST_CLONE event dispatch handling to sub referenced entities

Created on 29 May 2019, over 5 years ago
Updated 24 July 2024, 4 months ago

I'm looking to modify the values within the paragraphs of a cloned node before cloning.

I notice if I clone an entity, I can use my own event subscriber to subscribe to EntityCloneEvents::PRE_CLONE and EntityCloneEvents::POST_CLONE. This works great to modify fields on the cloned entity itself.

The dispatch code in EntityCloneForm::submitForm() handles this nicely:

    $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));

However, if the cloned entity has referenced entities (say, a paragraph), the event subscriber isn't fired. This code, in ContentEntityCloneBase::cloneReferencedEntities, doesn't have the event dispatch code:

        $cloned_reference = $referenced_entity->createDuplicate();
        /** @var \Drupal\entity_clone\EntityClone\EntityCloneInterface $entity_clone_handler */
        $entity_clone_handler = $this->entityTypeManager->getHandler($referenced_entity->getEntityTypeId(), 'entity_clone');
        $entity_clone_handler->cloneEntity($referenced_entity, $cloned_reference, $child_properties['children'], $already_cloned);

It looks like ContentEntityCloneBase doesn't easily inherit EntityCloneForm's EventDispacher, so it's not easy to add these extra PRE_CLONE and POST_CLONE lines there.

I could try to write up a quick patch for this, but am worried about how this eventDispatcher should best be passed down to the recursive levels and am hoping to get a little guidance.

Can eventDispatcher be passed as a parameter from EntityCloneForm:::submitForm() down to ContentEntityCloneBase::cloneEntity()? Or is there a more elegant way to pass that event dispatcher around the cloning process?

✨ Feature request
Status

Needs work

Version

2.0

Component

Code

Created by

🇨🇦Canada TrevorBradley

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024