By taran2l on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
11.2.x
Introduced in version:
11.2.0
Issue links:
Description:
New hooks are added that allow to control a new duplicate created by \Drupal\Core\Entity\EntityInterface::createDuplicate().
This can be used to change the duplicate entity generated by that method and for example allows to duplicate related field instances and entity displays.
class EntityHooks {
protected $duplicateSources = [];
/**
* Implements hook_entity_duplicate().
*/
#[Hook('entity_duplicate')]
public function entityDuplicate(EntityInterface $duplicate, EntityInterface $entity) : void {
if ($duplicate instanceof SomeInterface) {
$duplicate->set('name', $duplicate->label() . ' (duplicate)');
// Store the source so it can be used in the entity_insert hook.
$this->duplicateSources[$duplicate->uuid()] = $entity;
}
}
/**
* Implements hook_entity_insert().
*/
#[Hook('entity_insert')]
public function entityInsert(EntityInterface $entity) : void {
if ($entity instanceof SomeInterface && isset($this->duplicateSources[$duplicate->uuid()])) {
// Do something with based on the duplicate source, like cloning all variations.
}
}
}
This covers at least part of the functionality offered by projects such as https://drupal.org/project/replicate. These projects might be deprecated/updated to depend on its in favor of their own events and hooks.
Impacts:
Module developers