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..

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

pguillard created an issue. See original summary.

pguillard’s picture

Status: Active » Needs review
StatusFileSize
new1013 bytes

This patch is to be applied after #2706639: Support for sub-entity cloning #131

Then I can use a HOOK like that :

function HOOK_not_clonable_fields_alter(&$fields_list) {
  $fields_list = [
    'field_parent_node',
    'field_children_nodes',
    'state_publishing',
    'state_archiving'
  ];
}

Status: Needs review » Needs work

The last submitted patch, 2: fields_that_should_not_be_cloned-2953641-2.patch, failed testing. View results

pguillard’s picture

Status: Needs work » Needs review
StatusFileSize
new1013 bytes

partdigital made their first commit to this issue’s fork.

partdigital’s picture

Version: 8.x-1.x-dev » 2.x-dev

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

partdigital’s picture

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.

EDIT: It looks like some tests are failing in build that were working on local. Will address.

Other thoughts:

It would be great to be able to exclude/include fields right from the Field UI. For example, perhaps saved as third-party settings.

rajeshreeputra made their first commit to this issue’s fork.

rajeshreeputra’s picture

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

partdigital’s picture

StatusFileSize
new126.6 KB

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

https://www.drupal.org/project/entity_clone/issues/3350358 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.

Exclude from entity clone field settings.

partdigital’s picture

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']);
}
damien laguerre’s picture

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.

mably’s picture

Created another issue with some pretty generic hooks that could be useful: #3518541: New access hooks to allow fine grained configuration of the clone form

Any feedback will be appreciated.

partdigital’s picture

I've pushed up an update that addresses a performance issue.

deaom made their first commit to this issue’s fork.

deaom’s picture

Status: Needs review » Needs work

After re-basing the tests are failing. It does seem that the circular cloning no longer works as it does not clone that node, just references it. Looking into it, but do not mind if somebody will be faster then me.

deaom’s picture

Status: Needs work » Needs review

Test are now passing, ready for review.
There was an issue where PTs were getting cloned even if excluded, an issue that was already solved in #3397218: Clone form is not respecting 'cloneable_entities' config resulting in memory limit on recursive in ContentEntityCloneFormBase for a custom Entity.. The code for PT handling is from there, adjusted for this code.