class EntityCloneForm in Entity Clone 8
Implements an entity Clone form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\entity_clone\Form\EntityCloneForm
Expanded class hierarchy of EntityCloneForm
File
- src/
Form/ EntityCloneForm.php, line 23
Namespace
Drupal\entity_clone\FormView source
class EntityCloneForm extends FormBase {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity ready to clone.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $entity;
/**
* The entity type définition.
*
* @var \Drupal\Core\Entity\EntityTypeInterface
*/
protected $entityTypeDefinition;
/**
* The string translation manager.
*
* @var \Drupal\Core\StringTranslation\TranslationManager
*/
protected $stringTranslationManager;
/**
* Event dispatcher service.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\Messenger
*/
protected $messenger;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The entity clone settings manager service.
*
* @var \Drupal\entity_clone\EntityCloneSettingsManager
*/
protected $entityCloneSettingsManager;
/**
* The Service Provider that verifies if entity has ownership.
*
* @var \Drupal\entity_clone\Services\EntityCloneServiceProvider
*/
protected $serviceProvider;
/**
* Constructs a new Entity Clone form.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match service.
* @param \Drupal\Core\StringTranslation\TranslationManager $string_translation
* The string translation manager.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* The event dispatcher service.
* @param \Drupal\Core\Messenger\Messenger $messenger
* The messenger service.
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
* The current user.
* @param \Drupal\entity_clone\EntityCloneSettingsManager $entity_clone_settings_manager
* The entity clone settings manager.
* @param \Drupal\entity_clone\Services\EntityCloneServiceProvider $service_provider
* The Service Provider that verifies if entity has ownership.
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'entity_clone_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
if ($this->entity && $this->entityTypeDefinition
->hasHandlerClass('entity_clone')) {
/** @var \Drupal\entity_clone\EntityClone\EntityCloneFormInterface $entity_clone_handler */
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;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\entity_clone\EntityClone\EntityCloneInterface $entity_clone_handler */
$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);
}
/**
* Cancel form handler.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function cancelForm(array &$form, FormStateInterface $form_state) {
$this
->formSetRedirect($form_state, $this->entity);
}
/**
* Sets a redirect on form state.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The cloned 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>');
}
}
/**
* Gets the entity of this form.
*
* @return \Drupal\Core\Entity\EntityInterface
* The entity.
*/
public function getEntity() {
return $this->entity;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityCloneForm:: |
protected | property | The current user. | |
EntityCloneForm:: |
protected | property | The entity ready to clone. | |
EntityCloneForm:: |
protected | property | The entity clone settings manager service. | |
EntityCloneForm:: |
protected | property | The entity type définition. | |
EntityCloneForm:: |
protected | property | The entity type manager. | |
EntityCloneForm:: |
protected | property | Event dispatcher service. | |
EntityCloneForm:: |
protected | property |
The messenger service. Overrides MessengerTrait:: |
|
EntityCloneForm:: |
protected | property | The Service Provider that verifies if entity has ownership. | |
EntityCloneForm:: |
protected | property | The string translation manager. | |
EntityCloneForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
EntityCloneForm:: |
public | function | Cancel form handler. | |
EntityCloneForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
EntityCloneForm:: |
protected | function | Sets a redirect on form state. | |
EntityCloneForm:: |
public | function | Gets the entity of this form. | |
EntityCloneForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
EntityCloneForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
EntityCloneForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
EntityCloneForm:: |
public | function | Constructs a new Entity Clone form. | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |