Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ruloweb created an issue. See original summary.

ruloweb’s picture

Status: Needs review » Needs work

The last submitted patch, 2: entity_clone-event_dispatcher-2800203-2.patch, failed testing.

The last submitted patch, 2: entity_clone-event_dispatcher-2800203-2.patch, failed testing.

ruloweb’s picture

New patch. This one adds 4 events:

PRE_CLONE and POST_CLONE events run inside submitForm() method, before/after cloneEntity() method.

PRE_SAVE and POST_SAVE events run inside cloneEntity() method, before/after save() method. If you create an event subscriber which calls to cloneEntity(), this event turns recursive.

I managed to create a recursive solution for cloning Paragraphs and Field collections with these two last events #2706639-18: Support for sub-entity cloning.

Status: Needs review » Needs work

The last submitted patch, 5: entity_clone-event_dispatcher-2800203-5.patch, failed testing.

The last submitted patch, 5: entity_clone-event_dispatcher-2800203-5.patch, failed testing.

Erik Frèrejean’s picture

I actually was just working on a very similar patch, but going the "hook" way, using hooks very similar to the regular "presave"/"update" hooks. Just applied the patch in #5, which works like a charm. I however only have a need for the PRE_SAVE event, so haven't tested the others.

vpeltot’s picture

I'm OK to add the two PRE_CLONE and POST_CLONE events.
But for the PRE_SAVE and the POST_SAVE I think it's the core responsability to dispatch all entity operations (#2551893: Add events for matching entity hooks)

Rename the Events namespace and directory to Event (to respect all core modules subdirectory definition)

vpeltot’s picture

Status: Needs work » Needs review
Erik Frèrejean’s picture

@vpeltot, with regard to your PRE_SAVE/POST_SAVE comment I agree that these should be emitted by core, however my case (and I'm probably not the only one) I need to perform some changes on the entity pre save but only when a clone is happening. When relying on the core pre save action there is no way AFAIK to recognize that the entity is created from the clone action.

colan’s picture

#12: To handle that, I'm not sure if we should re-add the events or add something to the entity so that we know it's being cloned. Let's commit the other stuff first as we know we definitely need it. Then we can keep talking about this other thing.

I've made the following changes to match the style guidelines:

  • Used lowerCamelCase on the property name.
  • Added missing parameter descriptions.
colan’s picture

Some of the src/Form/EntityCloneForm.php includes conflict with (and are already handled by) #2908414: Error on abort and redirects - one patch to solve everything so that needs to be applied first. Here's what was changed. I can't run a real interdiff as it doesn't work.

< @@ -5,10 +5,12 @@ namespace Drupal\entity_clone\Form;
<  use Drupal\Core\Entity\EntityTypeManager;
<  use Drupal\Core\Form\FormBase;
---
> @@ -8,7 +8,10 @@ use Drupal\Core\Form\FormBase;
154d151
< -use Drupal\Core\Entity\Entity;

Tests will probably fail until #2908414: Error on abort and redirects - one patch to solve everything is committed.

Status: Needs review » Needs work
colan’s picture

Status: Needs work » Needs review

Resetting status for humans as we're expecting this to fail for the time being.

  • vpeltot committed 7e01336 on 8.x-1.x
    Issue #2800203 by colan, ruloweb, vpeltot: Event dispatcher for clone...
vpeltot’s picture

Status: Needs review » Fixed

Thanks @all

vpeltot’s picture

Status: Fixed » Closed (fixed)
cosmicdreams’s picture

So happy to have found this.

Is there any documentation that describes how you can integrate a custom entity into entity clone using these events? Would be great to see a code example of this.

jasonawant’s picture