You are here

protected function YamlFormEntityElementsValidator::validateHierarchy in YAML Form 8

Validate element hierarchy.

Return value

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

1 call to YamlFormEntityElementsValidator::validateHierarchy()
YamlFormEntityElementsValidator::validate in src/YamlFormEntityElementsValidator.php
Validate form elements.

File

src/YamlFormEntityElementsValidator.php, line 304

Class

YamlFormEntityElementsValidator
Defines a class to validate form elements.

Namespace

Drupal\yamlform

Code

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

    /** @var \Drupal\yamlform\YamlFormElementManagerInterface $element_manager */
    $element_manager = \Drupal::service('plugin.manager.yamlform.element');
    $plugin_id = $element_manager
      ->getElementPluginId($element);

    /** @var \Drupal\yamlform\YamlFormElementInterface $yamlform_element */
    $yamlform_element = $element_manager
      ->createInstance($plugin_id, $element);
    $t_args = [
      '%title' => !empty($element['#title']) ? $element['#title'] : $key,
      '@type' => $yamlform_element
        ->getTypeName(),
    ];
    if ($yamlform_element
      ->isRoot() && !empty($element['#yamlform_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 (!$yamlform_element
      ->isContainer($element) && !empty($element['#yamlform_children'])) {
      $messages[] = $this
        ->t('The %title (@type) is a form element that can not have any child elements.', $t_args);
    }
  }
  return $messages;
}