You are here

protected function WebformEntityElementsValidator::validateVariants in Webform 6.x

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

Validate that element are not deleted when the webform has related variants.

Return value

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

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

File

src/WebformEntityElementsValidator.php, line 456

Class

WebformEntityElementsValidator
Webform elements validator.

Namespace

Drupal\webform

Code

protected function validateVariants() {
  if (!$this->webform
    ->hasVariants()) {
    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) {
      if ($this->webform
        ->getVariants(NULL, NULL, $missing_element_key)
        ->count()) {
        $t_args = [
          '%title' => $this->webform
            ->label(),
          '%key' => $missing_element_key,
          ':href' => $this->webform
            ->toUrl('variants')
            ->toString(),
        ];
        $messages[] = $this
          ->t('The %key element can not be removed because the %title webform has related <a href=":href">variants</a>.', $t_args);
      }
    }
    return $messages;
  }
  return NULL;
}