You are here

protected function WebformEntityElementsValidator::validateHierarchy in Webform 6.x

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

Validate element hierarchy.

Return value

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

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

File

src/WebformEntityElementsValidator.php, line 493

Class

WebformEntityElementsValidator
Webform elements validator.

Namespace

Drupal\webform

Code

protected function validateHierarchy() {
  $elements = $this->webform
    ->getElementsInitializedAndFlattened();
  $messages = [];
  foreach ($elements as $key => $element) {
    $plugin_id = $this->elementManager
      ->getElementPluginId($element);

    /** @var \Drupal\webform\Plugin\WebformElementInterface $webform_element */
    $webform_element = $this->elementManager
      ->createInstance($plugin_id);
    $t_args = [
      '%title' => !empty($element['#title']) ? $element['#title'] : $key,
      '@type' => $webform_element
        ->getTypeName(),
    ];
    if ($webform_element
      ->isRoot() && !empty($element['#webform_parent_key'])) {
      $messages[] = $this
        ->t('The %title (@type) is a root element that can not be used as child to another element', $t_args);
    }
    elseif (!$webform_element
      ->isContainer($element) && !empty($element['#webform_children'])) {
      $messages[] = $this
        ->t('The %title (@type) is a webform element that can not have any child elements.', $t_args);
    }
    elseif ($plugin_id === 'webform_table_row') {
      $parent_element = $element['#webform_parent_key'] ? $elements[$element['#webform_parent_key']] : NULL;
      if (!$parent_element || !isset($parent_element['#type']) || $parent_element['#type'] !== 'webform_table') {
        $t_args += [
          '%parent_title' => $this
            ->t('Table'),
          '@parent_type' => 'webform_table',
        ];
        $messages[] = $this
          ->t('The %title (@type) must be with in a %parent_title (@parent_type) element.', $t_args);
      }
    }
  }
  return $messages;
}