View source
<?php
namespace Drupal\entity_clone\Form;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\TranslationManager;
use Drupal\entity_clone\Services\EntityCloneServiceProvider;
use Drupal\entity_clone\EntityCloneSettingsManager;
use Drupal\entity_clone\Event\EntityCloneEvent;
use Drupal\entity_clone\Event\EntityCloneEvents;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class EntityCloneForm extends FormBase {
protected $entityTypeManager;
protected $entity;
protected $entityTypeDefinition;
protected $stringTranslationManager;
protected $eventDispatcher;
protected $messenger;
protected $currentUser;
protected $entityCloneSettingsManager;
protected $serviceProvider;
public function __construct(EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $route_match, TranslationManager $string_translation, EventDispatcherInterface $eventDispatcher, Messenger $messenger, AccountProxyInterface $currentUser, EntityCloneSettingsManager $entity_clone_settings_manager, EntityCloneServiceProvider $service_provider) {
$this->entityTypeManager = $entity_type_manager;
$this->stringTranslationManager = $string_translation;
$this->eventDispatcher = $eventDispatcher;
$this->messenger = $messenger;
$parameter_name = $route_match
->getRouteObject()
->getOption('_entity_clone_entity_type_id');
$this->entity = $route_match
->getParameter($parameter_name);
$this->entityTypeDefinition = $entity_type_manager
->getDefinition($this->entity
->getEntityTypeId());
$this->currentUser = $currentUser;
$this->entityCloneSettingsManager = $entity_clone_settings_manager;
$this->serviceProvider = $service_provider;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('current_route_match'), $container
->get('string_translation'), $container
->get('event_dispatcher'), $container
->get('messenger'), $container
->get('current_user'), $container
->get('entity_clone.settings.manager'), $container
->get('entity_clone.service_provider'));
}
public function getFormId() {
return 'entity_clone_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
if ($this->entity && $this->entityTypeDefinition
->hasHandlerClass('entity_clone')) {
if ($this->entityTypeManager
->hasHandler($this->entityTypeDefinition
->id(), 'entity_clone_form')) {
$entity_clone_form_handler = $this->entityTypeManager
->getHandler($this->entityTypeDefinition
->id(), 'entity_clone_form');
$form = array_merge($form, $entity_clone_form_handler
->formElement($this->entity));
}
$entityType = $this
->getEntity()
->getEntityTypeId();
if ($this->serviceProvider
->entityTypeHasOwnerTrait($this
->getEntity()
->getEntityType()) && $this->currentUser
->hasPermission('take_ownership_on_clone ' . $entityType . ' entity')) {
$form['take_ownership'] = [
'#type' => 'checkbox',
'#title' => $this->stringTranslationManager
->translate('Take ownership'),
'#default_value' => $this->entityCloneSettingsManager
->getTakeOwnershipSetting(),
'#description' => $this->stringTranslationManager
->translate('Take ownership of the newly created cloned entity.'),
];
}
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['clone'] = [
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => $this->stringTranslationManager
->translate('Clone'),
];
$form['actions']['abort'] = [
'#type' => 'submit',
'#value' => $this->stringTranslationManager
->translate('Cancel'),
'#submit' => [
'::cancelForm',
],
];
}
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity_clone_handler = $this->entityTypeManager
->getHandler($this->entityTypeDefinition
->id(), 'entity_clone');
if ($this->entityTypeManager
->hasHandler($this->entityTypeDefinition
->id(), 'entity_clone_form')) {
$entity_clone_form_handler = $this->entityTypeManager
->getHandler($this->entityTypeDefinition
->id(), 'entity_clone_form');
}
$properties = [];
if (isset($entity_clone_form_handler) && $entity_clone_form_handler) {
$properties = $entity_clone_form_handler
->getValues($form_state);
}
$duplicate = $this->entity
->createDuplicate();
$this->eventDispatcher
->dispatch(EntityCloneEvents::PRE_CLONE, new EntityCloneEvent($this->entity, $duplicate, $properties));
$cloned_entity = $entity_clone_handler
->cloneEntity($this->entity, $duplicate, $properties);
$this->eventDispatcher
->dispatch(EntityCloneEvents::POST_CLONE, new EntityCloneEvent($this->entity, $duplicate, $properties));
$this->messenger
->addMessage($this->stringTranslationManager
->translate('The entity <em>@entity (@entity_id)</em> of type <em>@type</em> was cloned.', [
'@entity' => $this->entity
->label(),
'@entity_id' => $this->entity
->id(),
'@type' => $this->entity
->getEntityTypeId(),
]));
$this
->formSetRedirect($form_state, $cloned_entity);
}
public function cancelForm(array &$form, FormStateInterface $form_state) {
$this
->formSetRedirect($form_state, $this->entity);
}
protected function formSetRedirect(FormStateInterface $form_state, EntityInterface $entity) {
if ($entity && $entity
->hasLinkTemplate('canonical')) {
$form_state
->setRedirect($entity
->toUrl()
->getRouteName(), $entity
->toUrl()
->getRouteParameters());
}
else {
$form_state
->setRedirect('<front>');
}
}
public function getEntity() {
return $this->entity;
}
}