Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi guys, I think would be nice if there are some clone events to hook up, it allows not only to make custom modifications, but to third party modules add support to node clone in the D8 way, for example to support Paragraph or Field collection.
I created patch to start working on this feature, please have a look and make you patches/suggestions.
Thanks!
Comment | File | Size | Author |
---|---|---|---|
#14 | entity_clone-add_pre_and_post_clone_events-2800203-14.patch | 7.01 KB | colan |
#13 | entity_clone-add_pre_and_post_clone_events-2800203-13.patch | 7.12 KB | colan |
| |||
#13 | interdiff-2800203-10-13-do-not-test.diff | 1.52 KB | colan |
#10 | event_dispatcher_for-2800203-10.patch | 6.23 KB | vpeltot |
Comments
Comment #2
ruloweb CreditAttribution: ruloweb commentedComment #5
ruloweb CreditAttribution: ruloweb at Media.Monks commentedNew 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.
Comment #8
Erik FrèrejeanI 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.Comment #9
vpeltot CreditAttribution: vpeltot commentedI'm OK to add the two
PRE_CLONE
andPOST_CLONE
events.But for the
PRE_SAVE
and thePOST_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 toEvent
(to respect all core modules subdirectory definition)Comment #10
vpeltot CreditAttribution: vpeltot commentedComment #11
vpeltot CreditAttribution: vpeltot commentedComment #12
Erik Frèrejean@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.Comment #13
colan#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:
Comment #14
colanSome 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.Tests will probably fail until #2908414: Error on abort and redirects - one patch to solve everything is committed.
Comment #16
colanResetting status for humans as we're expecting this to fail for the time being.
Comment #18
vpeltot CreditAttribution: vpeltot commentedThanks @all
Comment #19
vpeltot CreditAttribution: vpeltot commentedComment #20
cosmicdreams CreditAttribution: cosmicdreams at Nerdery commentedSo 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.
Comment #21
jasonawantDocumentation patch found in #3063185: Add documentation for dispatched events