Add API to entity_clone

Created on 24 April 2018, over 6 years ago
Updated 26 August 2024, about 2 months ago

Problem/Motivation

I like to programmatic clone entities. Others like to intercept the cloning process.

- #2935803: Group clone

Checking the code there is no API file so it's hard to figure out how to do all this.

Proposed resolution

- Define an API (TBD).
- Add entity_clone.api.php file.

Remaining tasks

  1. Actually define and implement an API for doing clones programatically.
  2. Add tests for the new API.
  3. Port the existing form code to make use of the API.
  4. Update any form-related tests that have to change. Hopefully not.
  5. Document how the new API works.
  6. Review.
  7. RTBC.
  8. Commit.

User interface changes

Hopefully none.

API changes

TBD.

Data model changes

Hopefully none.

Feature request
Status

Needs work

Version

2.0

Component

Code

Created by

🇳🇱Netherlands clemens.tolboom Groningen, 🇳🇱/🇪🇺

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

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.

  • 🇧🇪Belgium dieterholvoet Brussels

    Rebased the MR against 2.x.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    29 pass
  • 🇬🇧United Kingdom jonathanshaw Stroud, UK

    However you do it, I think this work is untangling work is important and I will try to support it with reviews.

  • 🇧🇪Belgium dieterholvoet Brussels

    I'm getting a bit tired of rebasing this one, with every commit to the dev branch it's so much work. I hope we can get this one in soon, or I'll probably just stop updating entity_clone on my sites.

  • 🇧🇪Belgium dieterholvoet Brussels

    I squashed all commits to the issue fork to make it easier to rebase this one. For the same reason, I also undid some code style related changes. This one applies to 2.x again.

  • Pipeline finished with Failed
    4 months ago
    Total: 282s
    #214613
  • Status changed to Needs work 3 months ago
  • 🇮🇳India ankitv18

    Along with fixing phpunit failure, please check this also:https://git.drupalcode.org/issue/entity_clone-2965701/-/jobs/2021834#L47

  • Pipeline finished with Success
    3 months ago
    Total: 312s
    #219601
  • Status changed to Needs review 3 months ago
  • 🇧🇪Belgium dieterholvoet Brussels

    Pipeline is fixed!

  • 🇺🇸United States partdigital

    FWIW on our project we had to build an entity duplication service. We were originally using entity_clone but had to refactor the work because, unfortunately, entity_clone was not quite flexible enough. We ended up creating our own API which made it very flexible to define a duplication task. I’m happy to contribute in this (or another issue) if anyone is interested. (Note that we also have test coverage).

    To summarize these are the parts of our API.

    DuplicationStrategy
    This defines how we want our duplication to run. This is where we define which fields, entity types, bundles and even individual entities we want to include in the duplication process.

    DuplicationProcess
    This generates duplication process based on the entity and strategy that is passed to it. It essentially generates an array that details every step that needs to happen for successful duplication. We’ve used to this diagnose any issues before the duplication actually happens.

    DuplicationExecutable
    This actually executes the duplication process. It does this by reading the duplication process array and executing each step along the way. Because the duplication process is an array it’s very easy to perform it as a batch.

    Some remaining work:
    We need to add a way to undo/rollback the duplication process if there is an error. Doing it in a single request it’s very easy but doing it as a batch can be a bit trickier.

    Here is an example of the API:

    <?php
    // This defines a strategy that will duplicate nodes found in the “related_content” field but will exclude articles.
    $strategy = DuplicationStrategy::create($entity);
    $strategy->setEntityTypes(['node']);
    $strategy->addField(‘related_content’);
    $strategy->excludeBundles([‘article’]);

    // Pass the strategy to DuplicationProcess object. This will generate the array of all necessary steps.
    $process = new DuplicationProcess($entity, $strategy);
    $process->build();

    // Pass the process to the executable. This will execute all the steps in the process and return the duplicated entity.
    $executable = new DuplicationExecutable($process);
    $executable->execute();
    return $executable->getDuplicate();

    Just food for thought :D

  • Status changed to Needs work about 2 months ago
  • 🇧🇪Belgium joevagyok

    @partdigital can we have another PR with your proposal in a different branch on this issue for us to evaluate and choose?

Production build 0.71.5 2024