View source
<?php
namespace Drupal\webform_ui\Form;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Form\WebformDeleteFormBase;
use Drupal\webform\Plugin\WebformElementVariantInterface;
use Drupal\webform\Utility\WebformElementHelper;
use Drupal\webform\WebformInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformUiElementDeleteForm extends WebformDeleteFormBase {
protected $renderer;
protected $elementManager;
protected $elementsValidator;
protected $webform;
protected $webformElement;
protected $key;
protected $element;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->renderer = $container
->get('renderer');
$instance->elementManager = $container
->get('plugin.manager.webform.element');
$instance->elementsValidator = $container
->get('webform.elements_validator');
return $instance;
}
public function getQuestion() {
if ($this
->isDialog()) {
$t_args = [
'@title' => $this
->getElementTitle(),
];
return $this
->t("Delete the '@title' element?", $t_args);
}
else {
$t_args = [
'%webform' => $this->webform
->label(),
'%title' => $this
->getElementTitle(),
];
return $this
->t('Delete the %title element from the %webform webform?', $t_args);
}
}
public function getWarning() {
$t_args = [
'%title' => $this
->getElementTitle(),
];
return [
'#type' => 'webform_message',
'#message_type' => 'warning',
'#message_message' => $this
->t('Are you sure you want to delete the %title element?', $t_args) . '<br/>' . '<strong>' . $this
->t('This action cannot be undone.') . '</strong>',
];
}
public function getDescription() {
$element_plugin = $this
->getWebformElementPlugin();
$items = [];
$items[] = $this
->t('Remove this element');
$items[] = $this
->t('Delete any submission data associated with this element');
if ($element_plugin
->isContainer($this->element)) {
$items[] = $this
->t('Delete all child elements');
}
if ($element_plugin instanceof WebformElementVariantInterface) {
$items[] = $this
->t('Delete all related variants');
}
return [
'title' => [
'#markup' => $this
->t('This action will…'),
],
'list' => [
'#theme' => 'item_list',
'#items' => $items,
],
];
}
public function getDetails() {
$elements = $this
->getDeletedElementsItemList($this->element['#webform_children']);
if ($elements) {
return [
'#type' => 'details',
'#title' => $this
->t('Nested elements being deleted'),
'elements' => $elements,
];
}
else {
return [];
}
}
public function getCancelUrl() {
return $this->webform
->toUrl('edit-form');
}
public function getFormId() {
return 'webform_ui_element_delete_form';
}
public function buildForm(array $form, FormStateInterface $form_state, WebformInterface $webform = NULL, $key = NULL) {
$this->webform = $webform;
$this->key = $key;
$this->element = $webform
->getElement($key);
if ($this->element === NULL) {
throw new NotFoundHttpException();
}
$form = parent::buildForm($form, $form_state);
$form = $this
->buildDialogConfirmForm($form, $form_state);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->webform
->deleteElement($this->key);
$this->webform
->save();
$this
->messenger()
->addStatus($this
->t('The webform element %title has been deleted.', [
'%title' => $this
->getElementTitle(),
]));
$query = [];
if ($this
->getWebformElementPlugin() instanceof WebformElementVariantInterface) {
$query = [
'reload' => 'true',
];
}
$form_state
->setRedirectUrl($this->webform
->toUrl('edit-form', [
'query' => $query,
]));
}
protected function getDeletedElementsItemList(array $children) {
if (empty($children)) {
return [];
}
$items = [];
foreach ($children as $key) {
$element = $this->webform
->getElement($key);
if (isset($element['#title'])) {
$title = new FormattableMarkup('@title (@key)', [
'@title' => $element['#title'],
'@key' => $key,
]);
}
else {
$title = $key;
}
$items[$key]['title'] = [
'#markup' => $title,
];
if ($element['#webform_children']) {
$items[$key]['items'] = $this
->getDeletedElementsItemList($element['#webform_children']);
}
}
return [
'#theme' => 'item_list',
'#items' => $items,
];
}
protected function getElementTitle() {
return WebformElementHelper::getAdminTitle($this->element);
}
protected function getWebformElementPlugin() {
return $this->elementManager
->getElementInstance($this->element);
}
}