Possibility to set fields that should not be cloned

Created on 16 March 2018, almost 7 years ago
Updated 19 August 2024, 4 months ago

Based on #2706639: Support for sub-entity cloning , I guess it would be an interesting feature to have the possibility to set field names that should not be cloned.
My need is to avoid cloning fields like children nodes, scheduled update etc..

📌 Task
Status

Needs review

Version

1.0

Component

Code

Created by

🇫🇷France pguillard

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.

  • First commit to issue fork.
  • 🇺🇸United States partdigital

    partdigital changed the visibility of the branch 2953641-possibility-to-set to hidden.

  • Pipeline finished with Failed
    4 months ago
    Total: 383s
    #258571
  • 🇺🇸United States partdigital

    I was running into this issue as well. The entity_clone module does not make it very easy to customize which fields you want to include in the cloning process and which ones you want to exclude. I've created a new MR which introduces a new hook hook_entity_clone_clonable_fields_alter().

    How this works is that it scans and caches all the "cloneable" fields. Before that cache is saved though you can manipulate that array of fields with this hook.

    function mymodule_entity_clone_entity_clone_clonable_fields_alter(array &$fields) {
        // entity_clone will populate this array, you can modify it before it's cached.
        unset($fields['node']['page']['field_node_reference']);
    }
    

    Additional changes:

    This required refactoring EntitycloneClonableField, I have added more automated tests to support this refactoring effort. I can also confirm that all existing automated tests are still working.

  • First commit to issue fork.
  • 🇮🇳India rajeshreeputra Pune

    @partdigital, there is an issue Option to update entity data before cloning - UI Improvements Active for providing UI for updating data before cloning, we can utilize that.

  • 🇺🇸United States partdigital

    Hi @rajeshreeputra, thanks for adding the dependency injection. I had noticed that last night!

    https://www.drupal.org/project/entity_clone/issues/3350358 Option to update entity data before cloning - UI Improvements Active is not quite the same thing as this issue. This ticket is to allow you to specify which fields you want included/excluded in the cloning process. This would not be set by an author. Consider for example a node that has a "featured content" field. When we clone the node we wouldn't want to clone the content referenced in "featured content", this issue allows us to always exclude that field without requiring author input.

    This ticket is primarily an API change but expressed in terms of Field UI it would look something like this.

  • Pipeline finished with Failed
    4 months ago
    Total: 206s
    #259319
  • 🇺🇸United States partdigital

    I pushed up one change that makes it slightly easier to work with the hook. Now you can enable/disable fields like this.

    function mymodule_entity_clone_entity_clone_clonable_fields_alter(array &$fields) {
      // Mark the field as cloneable.
      $fields['node']['page']['field_node_reference'] = TRUE;
    
      // Mark the field as not cloneable.
      $fields['node']['page']['field_node_reference'] = FALSE;
      unset($fields['node']['page']['field_node_reference']);
    }
    
  • 🇫🇷France damien laguerre

    First, thanks for your works!

    I tried your patch and implemented the function mymodule_entity_clone_clonable_fields_alter(array &$fields)
    But that's not enough, for this to work I need to clear the new entity field using a bundle class with a custom createDuplicate:

      /**
       * {@inheritdoc}
       */
      public function createDuplicate() {
        $duplicate = parent::createDuplicate();
    
        $duplicate->set('field_paragraph', null);
    
        return $duplicate;
      }
    

    Otherwise, the field is not cloned by entity_clone, but is already filled by the core duplication.

Production build 0.71.5 2024