By bojanz on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x-1.x
Introduced in version:
8.x-1.0-rc2
Description:
Entities using Entity API permission and route providers can now have a duplicate form.
The entity needs to define a "duplicate" form and a "duplicate-form" link template.
The duplicate form should implement EntityDuplicateFormTrait and call $this->postSave() after saving the entity, to allow the EntityDuplicateEvent to be fired.
Example from tests:
* handlers = {
* "form" = {
* "add" = "\Drupal\entity_module_test\Form\EnhancedEntityForm",
* "edit" = "\Drupal\entity_module_test\Form\EnhancedEntityForm",
* "duplicate" = "\Drupal\entity_module_test\Form\EnhancedEntityForm",
* "delete" = "\Drupal\Core\Entity\EntityDeleteForm",
* },
* },
* links = {
* "add-page" = "/entity_test_enhanced/add",
* "add-form" = "/entity_test_enhanced/add/{type}",
* "edit-form" = "/entity_test_enhanced/{entity_test_enhanced}/edit",
* "duplicate-form" = "/entity_test_enhanced/{entity_test_enhanced}/duplicate",
* }
And in EnhancedEntityForm:
class EnhancedEntityForm extends ContentEntityForm {
use EntityDuplicateFormTrait;
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$this->entity->save();
$this->postSave($this->entity, $this->operation);
$this->messenger()->addMessage($this->t('Saved the %label enhanced entity.', ['%label' => $this->entity->label()]));
$form_state->setRedirect('entity.entity_test_enhanced.collection');
}
}
Note that if the duplicated entity is a bundle entity, its fields and (form/view) displays will be automatically duplicated as well.
Impacts:
Module developers