72 lines
2.2 KiB
PHP
72 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Drupal\webform;
|
|
|
|
use Drupal\Core\Entity\EntityInterface;
|
|
use Drupal\Core\Form\FormStateInterface;
|
|
use Drupal\Component\Uuid\Php;
|
|
use Drupal\Core\Url;
|
|
|
|
/**
|
|
* Provides a webform to duplicate existing submissions.
|
|
*/
|
|
class WebformSubmissionDuplicateForm extends WebformSubmissionForm {
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function setEntity(EntityInterface $entity) {
|
|
/** @var \Drupal\webform\WebformSubmissionInterface $entity */
|
|
$entity = clone $entity;
|
|
|
|
// Clear IDs.
|
|
$entity->set('uuid', (new Php())->generate());
|
|
$entity->set('sid', NULL);
|
|
$entity->set('serial', NULL);
|
|
|
|
// Clear state.
|
|
$entity->set('in_draft', FALSE);
|
|
|
|
// Create timestamps.
|
|
$entity->set('created', NULL);
|
|
$entity->set('changed', NULL);
|
|
$entity->set('completed', NULL);
|
|
|
|
// Clear admin notes and sticky.
|
|
$entity->set('notes', '');
|
|
$entity->set('sticky', FALSE);
|
|
|
|
$this->messageManager->setWebformSubmission($entity);
|
|
$this->messageManager->setWebform($entity->getWebform());
|
|
$this->messageManager->setSourceEntity($entity->getSourceEntity());
|
|
|
|
$this->entity = $entity;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Set webform state confirmation redirect and message.
|
|
*
|
|
* @param \Drupal\Core\Form\FormStateInterface $form_state
|
|
* The current state of the form.
|
|
*/
|
|
protected function setConfirmation(FormStateInterface $form_state) {
|
|
parent::setConfirmation($form_state);
|
|
|
|
// If the form is just reloading the duplicate form, redirect to the
|
|
// new submission.
|
|
$redirect = $form_state->getRedirect();
|
|
$route_name = $this->getRouteMatch()->getRouteName();
|
|
if ($redirect instanceof Url && $redirect->getRouteName() === $route_name) {
|
|
/** @var \Drupal\webform\WebformSubmissionInterface $entity */
|
|
$webform_submission = $this->entity;
|
|
$webform = $webform_submission->getWebform();
|
|
$source_entity = $webform_submission->getSourceEntity();
|
|
|
|
$redirect_route_name = $this->requestHandler->getRouteName($webform_submission, $source_entity, 'webform_submission.canonical');
|
|
$redirect_route_parameters = $this->requestHandler->getRouteParameters($webform_submission, $source_entity);
|
|
$form_state->setRedirect($redirect_route_name, $redirect_route_parameters);
|
|
}
|
|
}
|
|
|
|
}
|