You are here

protected function WebformEntityElementsValidator::validateSubmissions in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformEntityElementsValidator.php \Drupal\webform\WebformEntityElementsValidator::validateSubmissions()

Validate that element are not deleted when the webform has submissions.

Return value

array|null If not valid, an array of error messages.

1 call to WebformEntityElementsValidator::validateSubmissions()
WebformEntityElementsValidator::validate in src/WebformEntityElementsValidator.php
Validate webform elements.

File

src/WebformEntityElementsValidator.php, line 406

Class

WebformEntityElementsValidator
Webform elements validator.

Namespace

Drupal\webform

Code

protected function validateSubmissions() {
  if (!$this->webform
    ->hasSubmissions()) {
    return NULL;
  }
  $element_keys = [];
  if ($this->elements) {
    $this
      ->getElementKeysRecursive($this->elements, $element_keys);
  }
  $original_element_keys = [];
  if ($this->originalElements) {
    $this
      ->getElementKeysRecursive($this->originalElements, $original_element_keys);
  }
  if ($missing_element_keys = array_diff_key($original_element_keys, $element_keys)) {
    $messages = [];
    foreach ($missing_element_keys as $missing_element_key) {

      // Display an error message with 3 possible approaches to safely
      // deleting or hiding an element.
      $items = [];
      $items[] = $this
        ->t('<a href=":href">Delete all submissions</a> to this webform.', [
        ':href' => $this->webform
          ->toUrl('results-clear')
          ->toString(),
      ]);
      if (\Drupal::moduleHandler()
        ->moduleExists('webform_ui')) {
        $items[] = $this
          ->t('<a href=":href">Delete this individual element</a> using the webform UI.', [
          ':href' => Url::fromRoute('entity.webform_ui.element.delete_form', [
            'webform' => $this->webform
              ->id(),
            'key' => $missing_element_key,
          ])
            ->toString(),
        ]);
      }
      else {
        $items[] = $this
          ->t('<a href=":href">Enable the Webform UI module</a> and safely delete this element.', [
          ':href' => Url::fromRoute('system.modules_list')
            ->toString(),
        ]);
      }
      $items[] = $this
        ->t("Hide this element by setting its <code>'#access'</code> property to <code>false</code>.");
      $build = [
        'message' => [
          '#markup' => $this
            ->t('The %key element can not be removed because the %title webform has <a href=":href">results</a>.', [
            '%title' => $this->webform
              ->label(),
            '%key' => $missing_element_key,
            ':href' => $this->webform
              ->toUrl('results-submissions')
              ->toString(),
          ]),
        ],
        'items' => [
          '#theme' => 'item_list',
          '#items' => $items,
        ],
      ];
      $messages[] = $this->renderer
        ->renderPlain($build);
    }
    return $messages;
  }
  return NULL;
}