Add feature to clone entities by reference fields

Created on 1 June 2023, over 1 year ago

Problem/Motivation

Will be good to have a feature to clone entities by reference fields. For example, a node has an entity reference field with paragraphs, and when creating a clone of this entity - the related paragraphs should also be cloned.

Steps to reproduce

1. Get a node with several paragraphs.
2. Clone this node via Content Entity Clone.
3. Make an edit in a paragraph content on the cloned node.
4. See that those changes are applied to the source node too, but shouldn't, from the content editor's logic perspective.

Proposed resolution

Add an option "Clone related entities" to the reference fields.

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡¦πŸ‡²Armenia murz Yerevan, Armenia

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

Merge Requests

Comments & Activities

  • Issue created by @murz
  • πŸ‡¦πŸ‡²Armenia murz Yerevan, Armenia
  • πŸ‡¬πŸ‡§United Kingdom SirClickALot Somerset

    Agreed, but strictly only with an option in my opinion because often, we might others to clone our nodes that contain references to Paragraphs they they should not own copies of.

    For example, in our educational site we have Chapters of learning.
    Chapters of learning are broken up into Topics.
    Topics are rigorously-vetted content reviewed by senior education specialists.

    A Teacher might wish to make a copy of a Programme and make a variation of their own and that's fine but if they choose to keep one or more Topics then there is no way that they should have ownership of that content because we cannot then vouch for its quality.

    While I can see your requirement here, I also think it's beyond the scope of what this module aims to achieve which is merely to pre-fill a form with existing values, with the emphasis on the word values.

    By contrast, you may find that the Replicate UI β†’ module is what you are after because that one does exactly what you're asking for here and does so however deep the tree is.

  • πŸ‡¦πŸ‡²Armenia murz Yerevan, Armenia

    I agree that this should be optional and disabled by default.

    But still, it is the case for the current module because other solutions like the suggested "Replicate UI" or "Entity Clone" - immediately create a copy, instead of pre-filling the entity form.

    Seems, we still can pre-fill reference fields there too, if the form uses the "Inline Entity Form" widget type, to implement this feature.

    Because with that "Paragraphs" module, we can fill manually the node form, add several paragraphs there, and they will be stored in the form state, without creating actual entities until we click "Create" and submit the full form.

    Can we implement the same behavior by pre-filling referenced fields?

  • πŸ‡¦πŸ‡²Armenia murz Yerevan, Armenia

    And from the implementation point of view, seems we can create a new plugin, like the current "EntityLabelCloneSuffix", attached to only the "entity_reference" field type, that will do the thing.

  • πŸ‡ΊπŸ‡¦Ukraine Tolyan4ik Lviv

    Here is patch with Field processor to duplicate related entity.

  • @tolyan4ik opened merge request.
  • Status changed to Needs review over 1 year ago
  • πŸ‡ΊπŸ‡¦Ukraine Tolyan4ik Lviv
  • Status changed to RTBC over 1 year ago
  • πŸ‡¦πŸ‡²Armenia murz Yerevan, Armenia

    I've tested the proposed fix and it works pretty stable! A couple of renamings and minor fixes from my side have been pushed, so let's move to Reviewed status.

  • πŸ‡΅πŸ‡±Poland dmitry.korhov Poland, Warsaw
  • Status changed to Needs work 8 months ago
  • πŸ‡―πŸ‡΅Japan orakili

    This doesn't seem to work with some entity reference widgets like the media library or the inline entity form β†’ module.

    @Murz, would you be able to indicate with what widgets this plugin worked?

  • πŸ‡ΊπŸ‡ΈUnited States chrisolof

    I think the change in src/Plugin/FieldProcessorPluginBase.php is an unnecessary, breaking change that should be proposed in a separate issue. A change like that would normally go out in the next major version of a module so as not to break existing plugin implementations written against the 1.x plugin API.

  • πŸ‡ΊπŸ‡ΈUnited States duckydan

    For anyone that needs it, this already does this and has a really nice UI to manage how the entities are handled:

    https://www.drupal.org/project/entity_copy_reference β†’

  • πŸ‡ΊπŸ‡ΈUnited States chrisolof

    MR 9 opened. Builds on MR 3.

    - Uses the module's existing 1.x plugin definition API
    - Condenses the code a bit
    - Expands support to entity reference revisions fields

    Needs review and testing. Using this along with MR 8 (from ✨ Add ability to clone a layout builder layout field Active ) allows me to deeply clone layout builder layouts, including sub-entities referenced from layout builder content blocks inside of layout components. Pretty neat.

    Like in MR 8, it's possible we could use this module's clone process to clone these referenced entities when configured, falling back to the Drupal core-provided clone. That was not necessary for my project so I haven't gone that far here, but it certainly seems like a nice-to-have for projects that might need to configure how these referenced entities are cloned.

  • πŸ‡ΊπŸ‡ΈUnited States chrisolof

    The attached patch represents the state of MR 9 at the time of this comment.

Production build 0.71.5 2024