diff --git a/modules/webform_access/src/WebformAccessGroupDeleteForm.php b/modules/webform_access/src/WebformAccessGroupDeleteForm.php index 14fcece9..df937a1d 100644 --- a/modules/webform_access/src/WebformAccessGroupDeleteForm.php +++ b/modules/webform_access/src/WebformAccessGroupDeleteForm.php @@ -2,31 +2,34 @@ namespace Drupal\webform_access; -use Drupal\Core\Entity\EntityDeleteForm; -use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Url; -use Drupal\webform\Form\WebformDialogFormTrait; +use Drupal\webform\Form\WebformConfigEntityDeleteFormBase; /** * Provides a delete webform access group form. */ -class WebformAccessGroupDeleteForm extends EntityDeleteForm { - - use WebformDialogFormTrait; +class WebformAccessGroupDeleteForm extends WebformConfigEntityDeleteFormBase { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state) { - $form = parent::buildForm($form, $form_state); - return $this->buildDialogConfirmForm($form, $form_state); - } + protected $confirmCheckbox = FALSE; /** * {@inheritdoc} */ - public function getRedirectUrl() { - return Url::fromRoute('entity.webform_access_group.collection'); + public function getDescription() { + return [ + 'title' => [ + '#markup' => '' . $this->t('This action will…') . '', + ], + 'list' => [ + '#theme' => 'item_list', + '#items' => [ + $this->t('Remove configuration'), + $this->t('Affect any field which uses this access group'), + ], + ], + ]; } } diff --git a/modules/webform_access/src/WebformAccessTypeDeleteForm.php b/modules/webform_access/src/WebformAccessTypeDeleteForm.php index 70ed5193..b395647a 100644 --- a/modules/webform_access/src/WebformAccessTypeDeleteForm.php +++ b/modules/webform_access/src/WebformAccessTypeDeleteForm.php @@ -2,31 +2,34 @@ namespace Drupal\webform_access; -use Drupal\Core\Entity\EntityDeleteForm; -use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Url; -use Drupal\webform\Form\WebformDialogFormTrait; +use Drupal\webform\Form\WebformConfigEntityDeleteFormBase; /** * Provides a delete webform access type form. */ -class WebformAccessTypeDeleteForm extends EntityDeleteForm { - - use WebformDialogFormTrait; +class WebformAccessTypeDeleteForm extends WebformConfigEntityDeleteFormBase { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state) { - $form = parent::buildForm($form, $form_state); - return $this->buildDialogConfirmForm($form, $form_state); - } + protected $confirmCheckbox = FALSE; /** * {@inheritdoc} */ - public function getRedirectUrl() { - return Url::fromRoute('entity.webform_access_type.collection'); + public function getDescription() { + return [ + 'title' => [ + '#markup' => '' . $this->t('This action will…') . '', + ], + 'list' => [ + '#theme' => 'item_list', + '#items' => [ + $this->t('Remove configuration'), + $this->t('Affect any access group which uses this type'), + ], + ], + ]; } } diff --git a/modules/webform_image_select/src/WebformImageSelectImagesDeleteForm.php b/modules/webform_image_select/src/WebformImageSelectImagesDeleteForm.php index 85458cc3..ba030b5a 100644 --- a/modules/webform_image_select/src/WebformImageSelectImagesDeleteForm.php +++ b/modules/webform_image_select/src/WebformImageSelectImagesDeleteForm.php @@ -2,38 +2,53 @@ namespace Drupal\webform_image_select; -use Drupal\Core\Entity\EntityDeleteForm; -use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Url; -use Drupal\webform\Form\WebformDialogFormTrait; +use Drupal\webform\Form\WebformConfigEntityDeleteFormBase; /** * Provides a delete webform images select images form. */ -class WebformImageSelectImagesDeleteForm extends EntityDeleteForm { - - use WebformDialogFormTrait; +class WebformImageSelectImagesDeleteForm extends WebformConfigEntityDeleteFormBase { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state) { - $form = parent::buildForm($form, $form_state); + public function getDescription() { + return [ + 'title' => [ + '#markup' => '' . $this->t('This action will…') . '', + ], + 'list' => [ + '#theme' => 'item_list', + '#items' => [ + $this->t('Remove configuration'), + $this->t('Affect any element which uses these images'), + ], + ], + ]; + } + /** + * {@inheritdoc} + */ + public function getDetails() { /** @var \Drupal\webform\WebformOptionsInterface $webform_options */ $webform_options = $this->entity; /** @var \Drupal\webform_image_select\WebformImageSelectImagesStorageInterface $webform_images_storage */ $webform_images_storage = $this->entityTypeManager->getStorage('webform_image_select_images'); - // Display warning that options is used by webforms. - $t_args = ['%title' => $webform_options->label()]; + $t_args = [ + '%label' => $this->getEntity()->label(), + '@entity-type' => $this->getEntity()->getEntityType()->getLowercaseLabel(), + ]; + + $details = []; if ($used_by_webforms = $webform_images_storage->getUsedByWebforms($webform_options)) { - $form['used_by_composite_elements'] = [ + $details['used_by_composite_elements'] = [ '#type' => 'webform_message', '#message_message' => [ '#theme' => 'item_list', - '#title' => $this->t('%title is used by the below webform(s).', $t_args), + '#title' => $this->t('%label is used by the below webform(s).', $t_args), '#items' => $used_by_webforms, ], '#message_type' => 'warning', @@ -41,21 +56,15 @@ class WebformImageSelectImagesDeleteForm extends EntityDeleteForm { ]; } - $form['confirm'] = [ - '#type' => 'checkbox', - '#title' => $this->t('Yes, I want to delete these webform images.'), - '#required' => TRUE, - '#weight' => 10, - ]; - - return $this->buildDialogConfirmForm($form, $form_state); - } - - /** - * {@inheritdoc} - */ - public function getRedirectUrl() { - return Url::fromRoute('entity.webform_image_select_images.collection'); + if ($details) { + return [ + '#type' => 'details', + '#title' => $this->t('Webforms affected'), + ] + $details; + } + else { + return []; + } } } diff --git a/modules/webform_ui/src/Form/WebformUiElementDeleteForm.php b/modules/webform_ui/src/Form/WebformUiElementDeleteForm.php index 252b64ed..c267ea56 100644 --- a/modules/webform_ui/src/Form/WebformUiElementDeleteForm.php +++ b/modules/webform_ui/src/Form/WebformUiElementDeleteForm.php @@ -3,10 +3,9 @@ namespace Drupal\webform_ui\Form; use Drupal\Component\Render\FormattableMarkup; -use Drupal\Core\Form\ConfirmFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\RendererInterface; -use Drupal\webform\Form\WebformDialogFormTrait; +use Drupal\webform\Form\WebformDeleteFormBase; use Drupal\webform\Plugin\WebformElementManagerInterface; use Drupal\webform\WebformEntityElementsValidatorInterface; use Drupal\webform\WebformInterface; @@ -16,14 +15,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** * Webform for deleting a webform element. */ -class WebformUiElementDeleteForm extends ConfirmFormBase { - - use WebformDialogFormTrait; - - /** - * {@inheritdoc} - */ - protected $operation = 'delete'; +class WebformUiElementDeleteForm extends WebformDeleteFormBase { /** * The renderer. @@ -101,84 +93,80 @@ class WebformUiElementDeleteForm extends ConfirmFormBase { ); } + /****************************************************************************/ + // Delete form. + /****************************************************************************/ + /** * {@inheritdoc} */ - public function getDescription() { - $t_args = [ - '%element' => $this->getElementTitle(), - '%webform' => $this->webform->label(), - ]; - - $build = []; - $element_plugin = $this->getWebformElementPlugin(); - if ($element_plugin->isContainer($this->element)) { - $build['warning'] = [ - '#markup' => $this->t('This will immediately delete the %element container and all nested elements within %element from the %webform webform. This cannot be undone.', $t_args), - ]; + public function getQuestion() { + $t_args = ['%webform' => $this->webform->label(), '%title' => $this->getElementTitle()]; + if ($this->isDialog()) { + return $this->t('Delete the %title element?', $t_args); } else { - $build['warning'] = [ - '#markup' => $this->t('This will immediately delete the %element element from the %webform webform. This cannot be undone.', $t_args), - ]; - } - - if ($this->element['#webform_children']) { - $build['elements'] = $this->getDeletedElementsItemList($this->element['#webform_children']); - $build['elements']['#title'] = t('The below nested elements will be also deleted.'); + return $this->t('Delete the %title element from the %webform webform?', $t_args); } + } - return $this->renderer->renderPlain($build); + /** + * {@inheritdoc} + */ + 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) . '
' . + '' . $this->t('This action cannot be undone.') . '', + ]; } /** - * Get deleted elements as item list. - * - * @param array $children - * An array child key. - * - * @return array - * A render array representing an item list of elements. + * {@inheritdoc} */ - protected function getDeletedElementsItemList(array $children) { - if (empty($children)) { - return []; - } + public function getDescription() { + $element_plugin = $this->getWebformElementPlugin(); $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']); - } + $items[] = $this->t('Remove this element'); + $items[] = $this->t('Delete any submission associated with this element'); + if ($element_plugin->isContainer($this->element)) { + $items[] = $this->t('Delete all child elements'); } return [ - '#theme' => 'item_list', - '#items' => $items, + 'title' => [ + '#markup' => '' . $this->t('This action will…') . '', + ], + 'list' => [ + '#theme' => 'item_list', + '#items' => $items, + ], ]; } /** * {@inheritdoc} */ - public function getQuestion() { - return $this->t('Are you sure you want to delete the %title element from the %webform webform?', ['%webform' => $this->webform->label(), '%title' => $this->getElementTitle()]); + 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 []; + } } - /** - * {@inheritdoc} - */ - public function getConfirmText() { - return $this->t('Delete'); - } + /****************************************************************************/ + // Form methods. + /****************************************************************************/ /** * {@inheritdoc} @@ -222,6 +210,45 @@ class WebformUiElementDeleteForm extends ConfirmFormBase { $form_state->setRedirectUrl($this->webform->toUrl('edit-form')); } + /****************************************************************************/ + // Helper methods. + /****************************************************************************/ + + /** + * Get deleted elements as item list. + * + * @param array $children + * An array child key. + * + * @return array + * A render array representing an item list of elements. + */ + 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, + ]; + } + /** * Get the webform element's title or key. * diff --git a/modules/webform_ui/src/Form/WebformUiElementTypeSelectForm.php b/modules/webform_ui/src/Form/WebformUiElementTypeSelectForm.php index a1787455..baadbbb6 100644 --- a/modules/webform_ui/src/Form/WebformUiElementTypeSelectForm.php +++ b/modules/webform_ui/src/Form/WebformUiElementTypeSelectForm.php @@ -27,7 +27,7 @@ class WebformUiElementTypeSelectForm extends WebformUiElementTypeFormBase { if ($parent) { $parent_element = $webform->getElement($parent); $t_args = ['@parent' => $parent_element['#admin_title'] ?: $parent_element['#title'] ?: $parent_element['#webform_key']]; - $form['#title'] = t('Select an element to add to "@parent"', $t_args); + $form['#title'] = $this->t('Select an element to add to "@parent"', $t_args); } $elements = $this->elementManager->getInstances(); diff --git a/src/Form/AdminConfig/WebformAdminConfigExportersForm.php b/src/Form/AdminConfig/WebformAdminConfigExportersForm.php index c7a49abd..cbee6cec 100644 --- a/src/Form/AdminConfig/WebformAdminConfigExportersForm.php +++ b/src/Form/AdminConfig/WebformAdminConfigExportersForm.php @@ -127,10 +127,10 @@ class WebformAdminConfigExportersForm extends WebformAdminConfigBaseForm { // Copied from: system_check_directory(). $temp_directory = $form_state->getValue('temp_directory'); if (!is_dir($temp_directory) && !$this->fileSystem->mkdir($temp_directory, NULL, TRUE)) { - $form_state->setErrorByName('temp_directory', t('The directory %directory does not exist and could not be created.', ['%directory' => $temp_directory])); + $form_state->setErrorByName('temp_directory', $this->t('The directory %directory does not exist and could not be created.', ['%directory' => $temp_directory])); } if (is_dir($temp_directory) && !is_writable($temp_directory) && !$this->fileSystem->chmod($temp_directory)) { - $form_state->setErrorByName('temp_directory', t('The directory %directory exists but is not writable and could not be made writable.', ['%directory' => $temp_directory])); + $form_state->setErrorByName('temp_directory', $this->t('The directory %directory exists but is not writable and could not be made writable.', ['%directory' => $temp_directory])); } parent::validateForm($form, $form_state); } diff --git a/src/Form/WebformConfigEntityDeleteFormBase.php b/src/Form/WebformConfigEntityDeleteFormBase.php new file mode 100644 index 00000000..955af0a5 --- /dev/null +++ b/src/Form/WebformConfigEntityDeleteFormBase.php @@ -0,0 +1,200 @@ +entity->getEntityTypeId() . '_confirm_form'; + } + + /** + * {@inheritdoc} + */ + public function getQuestion() { + $t_args = [ + '@entity-type' => $this->getEntity()->getEntityType()->getLowercaseLabel(), + '%label' => $this->getEntity()->label(), + ]; + return $this->t('Delete %label @entity-type?', $t_args); + } + + /** + * Returns warning message to display. + * + * @return array + * A renderable array containing a warning message. + */ + public function getWarning() { + $t_args = [ + '@entity-type' => $this->getEntity()->getEntityType()->getLowercaseLabel(), + '%label' => $this->getEntity()->label(), + ]; + + return [ + '#type' => 'webform_message', + '#message_type' => 'warning', + '#message_message' => $this->t('Are you sure you want to delete the %label @entity-type?', $t_args) . '
' . + '' . $this->t('This action cannot be undone.') . '', + ]; + } + + /** + * {@inheritdoc} + */ + public function getDescription() { + return []; + } + + /** + * Returns details to display. + * + * @return array + * A renderable array containing details. + */ + public function getDetails() { + return []; + } + + /** + * Returns confirm input to display. + * + * @return array + * A renderable array containing confirm input. + */ + public function getConfirmInput() { + $t_args = [ + '@entity-type' => $this->getEntity()->getEntityType()->getLowercaseLabel(), + '%label' => $this->getEntity()->label(), + ]; + + if ($this->confirmCheckbox) { + return [ + '#type' => 'checkbox', + '#title' => $this->t('Yes, I want to delete the %label @entity-type', $t_args), + '#required' => TRUE, + ]; + } + else { + return []; + } + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return $this->t('Delete'); + } + + /** + * {@inheritdoc} + */ + public function getCancelText() { + return $this->t('Cancel'); + } + + /** + * {@inheritdoc} + */ + public function getFormName() { + return 'webform_config_entity_delete'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $form = parent::buildForm($form, $form_state); + $form['#attributes']['class'][] = 'confirmation'; + $form['#theme'] = 'confirm_form'; + $form[$this->getFormName()] = ['#type' => 'hidden', '#value' => 1]; + + // Title. + $form['#title'] = $this->getQuestion(); + + // Warning. + $form['warning'] = $this->getWarning(); + + // Description. + $form['description'] = $this->getDescription(); + + // Details and confirm input. + $details = $this->getDetails(); + $confirm_input = $this->getConfirmInput(); + if ($details) { + $form['details'] = $details; + } + if (!$details && $confirm_input) { + $form['hr'] = ['#markup' => '


']; + } + if ($confirm_input) { + $form['confirm'] = $confirm_input; + } + + // Dialog. + return $this->buildDialogConfirmForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + protected function actions(array $form, FormStateInterface $form_state) { + return [ + 'submit' => [ + '#type' => 'submit', + '#value' => $this->getConfirmText(), + '#submit' => [ + [$this, 'submitForm'], + ], + ], + 'cancel' => ConfirmFormHelper::buildCancelLink($this, $this->getRequest()), + ]; + } + + /** + * {@inheritdoc} + * + * The save() method is not used in EntityConfirmFormBase. This overrides the + * default implementation that saves the entity. + * + * Confirmation forms should override submitForm() instead for their logic. + */ + public function save(array $form, FormStateInterface $form_state) {} + + /** + * {@inheritdoc} + * + * The delete() method is not used in EntityConfirmFormBase. This overrides + * the default implementation that redirects to the delete-form confirmation + * form. + * + * Confirmation forms should override submitForm() instead for their logic. + */ + public function delete(array $form, FormStateInterface $form_state) {} + +} diff --git a/src/Form/WebformDeleteFormBase.php b/src/Form/WebformDeleteFormBase.php new file mode 100644 index 00000000..7f0ef35d --- /dev/null +++ b/src/Form/WebformDeleteFormBase.php @@ -0,0 +1,114 @@ +getFormName()] = ['#type' => 'hidden', '#value' => 1]; + + // Title. + $form['#title'] = $this->getQuestion(); + + // Warning. + $form['warning'] = $this->getWarning(); + + // Description. + $form['description'] = $this->getDescription(); + + // Details and confirm input. + $details = $this->getDetails(); + $confirm_input = $this->getConfirmInput(); + if ($details) { + $form['details'] = $details; + } + if (!$details && $confirm_input) { + $form['hr'] = ['#markup' => '


']; + } + if ($confirm_input) { + $form['confirm'] = $confirm_input; + } + + // Actions. + $form['actions'] = ['#type' => 'actions']; + $form['actions']['submit'] = [ + '#type' => 'submit', + '#value' => $this->getConfirmText(), + '#button_type' => 'primary', + ]; + $form['actions']['cancel'] = ConfirmFormHelper::buildCancelLink($this, $this->getRequest()); + + return $this->buildDialogConfirmForm($form, $form_state); + } + + /** + * Returns warning message to display. + * + * @return array + * A renderable array containing a warning message. + */ + public function getWarning() { + return [ + '#type' => 'webform_message', + '#message_type' => 'warning', + '#message_message' => $this->t('Are you sure you want to delete this?') . '
' . + '' . $this->t('This action cannot be undone.') . '', + ]; + } + + /** + * {@inheritdoc} + */ + public function getDescription() { + return []; + } + + /** + * Returns details to display. + * + * @return array + * A renderable array containing details. + */ + public function getDetails() { + return []; + } + + /** + * Returns confirm input to display. + * + * @return array + * A renderable array containing confirm input. + */ + public function getConfirmInput() { + return []; + } + + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return $this->t('Delete'); + } + +} diff --git a/src/Form/WebformHandlerDeleteForm.php b/src/Form/WebformHandlerDeleteForm.php index 6a35b714..1ee10fd7 100644 --- a/src/Form/WebformHandlerDeleteForm.php +++ b/src/Form/WebformHandlerDeleteForm.php @@ -2,16 +2,13 @@ namespace Drupal\webform\Form; -use Drupal\Core\Form\ConfirmFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\webform\WebformInterface; /** * Form for deleting a webform handler. */ -class WebformHandlerDeleteForm extends ConfirmFormBase { - - use WebformDialogFormTrait; +class WebformHandlerDeleteForm extends WebformDeleteFormBase { /** * The webform containing the webform handler to be deleted. @@ -31,16 +28,49 @@ class WebformHandlerDeleteForm extends ConfirmFormBase { * {@inheritdoc} */ public function getQuestion() { - return $this->t('Are you sure you want to delete the @handler handler from the %webform webform?', ['%webform' => $this->webform->label(), '@handler' => $this->webformHandler->label()]); + $t_args = ['%webform' => $this->webform->label(), '%title' => $this->webformHandler->label()]; + if ($this->isDialog()) { + return $this->t('Delete the %title handler?', $t_args); + } + else { + return $this->t('Delete the %title handler from the %webform webform?', $t_args); + } } /** * {@inheritdoc} */ - public function getConfirmText() { - return $this->t('Delete'); + public function getWarning() { + $t_args = ['%title' => $this->webformHandler->label()]; + return [ + '#type' => 'webform_message', + '#message_type' => 'warning', + '#message_message' => $this->t('Are you sure you want to delete the %title handler?', $t_args) . '
' . + '' . $this->t('This action cannot be undone.') . '', + ]; } + /** + * {@inheritdoc} + */ + public function getDescription() { + return [ + 'title' => [ + '#markup' => '' . $this->t('This action will…') . '', + ], + 'list' => [ + '#theme' => 'item_list', + '#items' => [ + $this->t('Remove this handler') + ], + ], + ]; + } + + /****************************************************************************/ + // Form methods. + /****************************************************************************/ + /** * {@inheritdoc} */ @@ -62,9 +92,7 @@ class WebformHandlerDeleteForm extends ConfirmFormBase { $this->webform = $webform; $this->webformHandler = $this->webform->getHandler($webform_handler); - $form = parent::buildForm($form, $form_state); - $this->buildDialogConfirmForm($form, $form_state); - return $form; + return parent::buildForm($form, $form_state); } /** diff --git a/src/Form/WebformResultsClearForm.php b/src/Form/WebformResultsClearForm.php index 8043fad3..c7ee27c8 100644 --- a/src/Form/WebformResultsClearForm.php +++ b/src/Form/WebformResultsClearForm.php @@ -2,6 +2,8 @@ namespace Drupal\webform\Form; +use Drupal\Core\Form\FormStateInterface; + /** * Webform for webform results clear webform. */ @@ -18,13 +20,78 @@ class WebformResultsClearForm extends WebformSubmissionsDeleteFormBase { * {@inheritdoc} */ public function getQuestion() { - if ($this->sourceEntity) { - $t_args = ['%title' => $this->sourceEntity->label()]; + $t_args = ['%label' => $this->getLabel()]; + return $this->t('Clear all %label submissions?', $t_args); + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $submission_total = $this->submissionStorage->getTotal($this->webform, $this->sourceEntity); + if ($submission_total) { + return parent::buildForm($form, $form_state); } else { - $t_args = ['%title' => $this->webform->label()]; + $t_args = ['%label' => $this->getLabel()]; + $form['message'] = [ + '#type' => 'webform_message', + '#message_type' => 'error', + '#message_message' => $this->t('There are no %label submissions.', $t_args), + ]; + return $form; } - return $this->t('Are you sure you want to delete all submissions to the %title webform?', $t_args); + } + + /** + * {@inheritdoc} + */ + public function getWarning() { + $t_args = ['%label' => $this->getLabel()]; + return [ + '#type' => 'webform_message', + '#message_type' => 'warning', + '#message_message' => $this->t('Are you sure you want to clear all %label submissions?', $t_args) . '
' . + '' . $this->t('This action cannot be undone.') . '', + ]; + } + + /** + * {@inheritdoc} + */ + public function getDescription() { + $submission_total = $this->submissionStorage->getTotal($this->webform, $this->sourceEntity); + + $t_args = [ + '%label' => $this->getLabel(), + '@total' => $submission_total, + '@submissions' => $this->formatPlural($submission_total, 'submission', 'submissions'), + ]; + + return [ + 'title' => [ + '#markup' => '' . $this->t('This action will…') . '', + ], + 'list' => [ + '#theme' => 'item_list', + '#items' => [ + $this->t('Remove @total %label @submissions', $t_args), + ['#markup' => '' . $this->t('May take a few minutes to complete') . ''], + ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + public function getConfirmInput() { + $t_args = ['%label' => $this->getLabel()]; + return [ + '#type' => 'checkbox', + '#title' => $this->t('Yes, I want to clear all %label submissions', $t_args), + '#required' => TRUE, + ]; } /** @@ -38,13 +105,8 @@ class WebformResultsClearForm extends WebformSubmissionsDeleteFormBase { * {@inheritdoc} */ public function getMessage() { - if ($this->sourceEntity) { - $t_args = ['%title' => $this->sourceEntity->label()]; - } - else { - $t_args = ['%title' => $this->webform->label()]; - } - $this->t('Webform %title submissions cleared.', $t_args); + $t_args = ['%label' => $this->getLabel()]; + $this->t('Webform %label submissions cleared.', $t_args); } } diff --git a/src/Form/WebformSubmissionDeleteForm.php b/src/Form/WebformSubmissionDeleteForm.php index d39ba796..d21ab62a 100644 --- a/src/Form/WebformSubmissionDeleteForm.php +++ b/src/Form/WebformSubmissionDeleteForm.php @@ -71,7 +71,12 @@ class WebformSubmissionDeleteForm extends ContentEntityDeleteForm { public function buildForm(array $form, FormStateInterface $form_state) { list($this->webformSubmission, $this->sourceEntity) = $this->requestHandler->getWebformSubmissionEntities(); $this->webform = $this->webformSubmission->getWebform(); - return parent::buildForm($form, $form_state); + + $form['warning'] = $this->getWarning(); + $form = parent::buildForm($form, $form_state); + $form['description'] = $this->getDescription(); + + return $form; } /** @@ -101,7 +106,53 @@ class WebformSubmissionDeleteForm extends ContentEntityDeleteForm { * {@inheritdoc} */ public function getQuestion() { - return $this->t('Are you sure you want to delete @title?', ['@title' => $this->webformSubmission->label()]); + $t_args = [ + '@entity-type' => $this->getEntity()->getEntityType()->getLowercaseLabel(), + '%label' => $this->getEntity()->label(), + ]; + return $this->t('Delete %label @entity-type?', $t_args); + } + + /** + * Returns warning message to display. + * + * @return array + * A renderable array containing a warning message. + */ + public function getWarning() { + $t_args = [ + '@entity-type' => $this->getEntity()->getEntityType()->getLowercaseLabel(), + '%label' => $this->getEntity()->label(), + ]; + + return [ + '#type' => 'webform_message', + '#message_type' => 'warning', + '#message_message' => $this->t('Are you sure you want to delete the %label @entity-type?', $t_args) . '
' . + '' . $this->t('This action cannot be undone.') . '', + ]; + } + + /** + * {@inheritdoc} + */ + public function getDescription() { + $t_args = [ + '@entity-type' => $this->getEntity()->getEntityType()->getLowercaseLabel(), + ]; + return [ + 'title' => [ + '#markup' => '' . $this->t('Deleting this @entity-type will…', $t_args) . '', + ], + 'list' => [ + '#theme' => 'item_list', + '#items' => [ + $this->t('Remove records from the data'), + $this->t('Delete any uploaded files'), + $this->t('Cancel all pending action'), + ], + ], + ]; } /** diff --git a/src/Form/WebformSubmissionsDeleteFormBase.php b/src/Form/WebformSubmissionsDeleteFormBase.php index 85fa859f..e026385a 100644 --- a/src/Form/WebformSubmissionsDeleteFormBase.php +++ b/src/Form/WebformSubmissionsDeleteFormBase.php @@ -4,7 +4,6 @@ namespace Drupal\webform\Form; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; -use Drupal\Core\Form\ConfirmFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\webform\WebformInterface; use Drupal\webform\WebformRequestInterface; @@ -13,7 +12,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** * Base webform for deleting webform submission. */ -abstract class WebformSubmissionsDeleteFormBase extends ConfirmFormBase { +abstract class WebformSubmissionsDeleteFormBase extends WebformDeleteFormBase { /** * Default number of submission to be deleted during batch processing. @@ -61,6 +60,8 @@ abstract class WebformSubmissionsDeleteFormBase extends ConfirmFormBase { public function __construct(EntityTypeManagerInterface $entity_type_manager, WebformRequestInterface $request_handler) { $this->submissionStorage = $entity_type_manager->getStorage('webform_submission'); $this->requestHandler = $request_handler; + + list($this->webform, $this->sourceEntity) = $this->requestHandler->getWebformEntities(); } /** @@ -80,14 +81,6 @@ abstract class WebformSubmissionsDeleteFormBase extends ConfirmFormBase { return $this->t('Clear'); } - /** - * {@inheritdoc} - */ - public function buildForm(array $form, FormStateInterface $form_state) { - list($this->webform, $this->sourceEntity) = $this->requestHandler->getWebformEntities(); - return parent::buildForm($form, $form_state); - } - /** * {@inheritdoc} */ @@ -102,6 +95,24 @@ abstract class WebformSubmissionsDeleteFormBase extends ConfirmFormBase { } } + /** + * Get webform or source entity label. + * + * @return null|string + * Webform or source entity label. + */ + public function getLabel() { + if ($this->sourceEntity) { + return $this->sourceEntity->label(); + } + elseif ($this->webform->label()) { + return $this->webform->label(); + } + else { + return ''; + } + } + /** * Message to displayed after submissions are deleted. * @@ -112,6 +123,10 @@ abstract class WebformSubmissionsDeleteFormBase extends ConfirmFormBase { return $this->t('Webform submissions cleared.'); } + /****************************************************************************/ + // Batch API. + /****************************************************************************/ + /** * Batch API; Initialize batch operations. * diff --git a/src/Form/WebformSubmissionsPurgeForm.php b/src/Form/WebformSubmissionsPurgeForm.php index 040c7650..7968bbfa 100644 --- a/src/Form/WebformSubmissionsPurgeForm.php +++ b/src/Form/WebformSubmissionsPurgeForm.php @@ -21,36 +21,43 @@ class WebformSubmissionsPurgeForm extends WebformSubmissionsDeleteFormBase { * {@inheritdoc} */ public function getQuestion() { - return $this->t('Are you sure you want to delete all submissions?'); + return $this->t('Purge all submissions?'); } /** * {@inheritdoc} */ - public function getConfirmText() { - return $this->t('Purge'); - } - - /** - * {@inheritdoc} - */ - public function getCancelUrl() { - return new Url('entity.webform_submission.collection'); + public function buildForm(array $form, FormStateInterface $form_state) { + $submission_total = \Drupal::entityQuery('webform_submission')->count()->execute(); + if ($submission_total) { + return parent::buildForm($form, $form_state); + } + else { + $form['message'] = [ + '#type' => 'webform_message', + '#message_type' => 'error', + '#message_message' => $this->t('There are no webform submissions.'), + ]; + return $form; + } } /** * {@inheritdoc} */ - public function getFinishedMessage() { - return $this->t('Webform submissions purged.'); + public function getWarning() { + return [ + '#type' => 'webform_message', + '#message_type' => 'warning', + '#message_message' => $this->t('Are you sure you want to purge all submissions?') . '
' . + '' . $this->t('This action cannot be undone.') . '', + ]; } /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state) { - $form = parent::buildForm($form, $form_state); - + public function getDescription() { $submission_total = \Drupal::entityQuery('webform_submission')->count()->execute(); $form_total = \Drupal::entityQuery('webform')->count()->execute(); @@ -61,14 +68,50 @@ class WebformSubmissionsPurgeForm extends WebformSubmissionsDeleteFormBase { '@forms' => $this->formatPlural($form_total, 'webform', 'webforms'), ]; - $form['confirm'] = [ + return [ + 'title' => [ + '#markup' => '' . $this->t('This action will…') . '', + ], + 'list' => [ + '#theme' => 'item_list', + '#items' => [ + $this->t('Remove @submission_total @submissions in @form_total @forms', $t_args), + ['#markup' => '' . $this->t('May take a few minutes to complete') . ''], + ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + public function getConfirmInput() { + return [ '#type' => 'checkbox', - '#title' => $this->t('Are you sure you want to delete @submission_total @submissions in @form_total @forms?', $t_args), + '#title' => $this->t('Yes, I want to purge all submissions'), '#required' => TRUE, - '#weight' => -10, ]; + } - return $form; + /** + * {@inheritdoc} + */ + public function getConfirmText() { + return $this->t('Purge'); + } + + /** + * {@inheritdoc} + */ + public function getCancelUrl() { + return new Url('entity.webform_submission.collection'); + } + + /** + * {@inheritdoc} + */ + public function getFinishedMessage() { + return $this->t('Webform submissions purged.'); } } diff --git a/src/WebformEntityDeleteForm.php b/src/WebformEntityDeleteForm.php index 7bb560e2..3d64c2a9 100644 --- a/src/WebformEntityDeleteForm.php +++ b/src/WebformEntityDeleteForm.php @@ -2,38 +2,30 @@ namespace Drupal\webform; -use Drupal\Core\Entity\EntityDeleteForm; -use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Url; -use Drupal\webform\Form\WebformDialogFormTrait; +use Drupal\webform\Form\WebformConfigEntityDeleteFormBase; /** * Provides a delete webform form. */ -class WebformEntityDeleteForm extends EntityDeleteForm { - - use WebformDialogFormTrait; +class WebformEntityDeleteForm extends WebformConfigEntityDeleteFormBase { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state) { - $form = parent::buildForm($form, $form_state); - $form['confirm'] = [ - '#type' => 'checkbox', - '#title' => $this->t('Yes, I want to delete this webform.'), - '#required' => TRUE, - '#weight' => 10, + public function getDescription() { + return [ + 'title' => [ + '#markup' => '' . $this->t('This action will…') . '', + ], + 'list' => [ + '#theme' => 'item_list', + '#items' => [ + $this->t('Remove configuration'), + $this->t('Delete all related submissions'), + $this->t('Affect any field or node which references this webform'), + ], + ], ]; - - return $this->buildDialogConfirmForm($form, $form_state); - } - - /** - * {@inheritdoc} - */ - public function getRedirectUrl() { - return Url::fromRoute('entity.webform.collection'); } } diff --git a/src/WebformHelpManager.php b/src/WebformHelpManager.php index 0443c4c0..b58effad 100644 --- a/src/WebformHelpManager.php +++ b/src/WebformHelpManager.php @@ -1732,18 +1732,6 @@ class WebformHelpManager implements WebformHelpManagerInterface { ], ]; - // Submissions: Purge. - $help['submissions_purge'] = [ - 'group' => 'submissions', - 'title' => $this->t('Submissions: Purge'), - 'content' => $this->t('The Submissions purge page allows all submissions across all webforms to be deleted. PLEASE NOTE: THIS ACTION CANNOT BE UNDONE.'), - 'message_type' => 'warning', - 'routes' => [ - // @see /admin/structure/webform/results/purge - 'entity.webform_submission.collection_purge', - ], - ]; - // Submissions: Log. $help['submissions_log'] = [ 'group' => 'submissions', @@ -1755,7 +1743,6 @@ class WebformHelpManager implements WebformHelpManagerInterface { ], ]; - // Results. $help['results'] = [ 'group' => 'submissions', @@ -1791,17 +1778,6 @@ class WebformHelpManager implements WebformHelpManagerInterface { ], ]; - // Results: Clear. - $help['results_clear'] = [ - 'group' => 'submissions', - 'title' => $this->t('Results: Clear'), - 'content' => $this->t("The Clear page allows all submissions to a webform to be deleted."), - 'routes' => [ - // @see /admin/structure/webform/manage/{webform}/results/clear - 'entity.webform.results_clear', - ], - ]; - /**************************************************************************/ // Submission. /**************************************************************************/ @@ -2004,15 +1980,6 @@ class WebformHelpManager implements WebformHelpManagerInterface { 'entity.node.webform.results_export', ], ]; - $help['webform_node_results_clear'] = [ - 'group' => 'webform_nodes', - 'title' => $this->t('Webform Node: Results: Clear'), - 'content' => $this->t("The Clear page allows all submissions to a webform node to be deleted."), - 'routes' => [ - // @see /node/{node}/webform/results/clear - 'entity.node.webform.results_clear', - ], - ]; // Webform Block. $help['webform_block'] = [ diff --git a/src/WebformOptionsDeleteForm.php b/src/WebformOptionsDeleteForm.php index 5215faf5..5d6aedc4 100644 --- a/src/WebformOptionsDeleteForm.php +++ b/src/WebformOptionsDeleteForm.php @@ -2,72 +2,71 @@ namespace Drupal\webform; -use Drupal\Core\Entity\EntityDeleteForm; -use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Url; -use Drupal\webform\Form\WebformDialogFormTrait; +use Drupal\webform\Form\WebformConfigEntityDeleteFormBase; /** * Provides a delete webform options form. */ -class WebformOptionsDeleteForm extends EntityDeleteForm { - - use WebformDialogFormTrait; +class WebformOptionsDeleteForm extends WebformConfigEntityDeleteFormBase { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state) { - $form = parent::buildForm($form, $form_state); + public function getDescription() { + return [ + 'title' => [ + '#markup' => '' . $this->t('This action will…') . '', + ], + 'list' => [ + '#theme' => 'item_list', + '#items' => [ + $this->t('Remove configuration'), + $this->t('Affect any element which uses these options'), + ], + ], + ]; + } + /** + * {@inheritdoc} + */ + public function getDetails() { /** @var \Drupal\webform\WebformOptionsInterface $webform_options */ $webform_options = $this->entity; /** @var \Drupal\webform\WebformOptionsStorageInterface $webform_options_storage */ $webform_options_storage = $this->entityTypeManager->getStorage('webform_options'); - // Display warning that options is used by composite elements - // and/or webforms. - $t_args = ['%title' => $webform_options->label()]; - $message = []; + $t_args = [ + '%label' => $this->getEntity()->label(), + '@entity-type' => $this->getEntity()->getEntityType()->getLowercaseLabel(), + ]; + + $details = []; if ($used_by_elements = $webform_options_storage->getUsedByCompositeElements($webform_options)) { - $message['elements'] = [ + $details['elements'] = [ '#theme' => 'item_list', - '#title' => $this->t('%title is used by the below composite element(s).', $t_args), + '#title' => $this->t('%label is used by the below composite element(s).', $t_args), '#items' => $used_by_elements, ]; } if ($used_by_webforms = $webform_options_storage->getUsedByWebforms($webform_options)) { - $message['webform'] = [ + $details['webform'] = [ '#theme' => 'item_list', - '#title' => $this->t('%title is used by the below webform(s).', $t_args), + '#title' => $this->t('%label is used by the below webform(s).', $t_args), '#items' => $used_by_webforms, ]; } - if ($message) { - $form['used_by_composite_elements'] = [ - '#type' => 'webform_message', - '#message_message' => $message, - '#message_type' => 'warning', - '#weight' => -100, - ]; - } - - $form['confirm'] = [ - '#type' => 'checkbox', - '#title' => $this->t('Yes, I want to delete these webform options.'), - '#required' => TRUE, - '#weight' => 10, - ]; - - return $this->buildDialogConfirmForm($form, $form_state); - } - /** - * {@inheritdoc} - */ - public function getRedirectUrl() { - return Url::fromRoute('entity.webform_options.collection'); + if ($details) { + return [ + '#type' => 'details', + '#title' => $this->t('Webforms affected'), + ] + $details; + } + else { + return []; + } } }