- First commit to issue fork.
- 🇺🇸United States partdigital
partdigital → changed the visibility of the branch 2953641-possibility-to-set to hidden.
- Merge request !87Issue #2953641: Add hook_entity_clone_clonable_fields_alter(). → (Open) created by partdigital
- 🇺🇸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.
- 🇺🇸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.