You are here

protected function YamlFormElementBase::setConfigurationFormDefaultValueRecursive in YAML Form 8

Set configuration form default values recursively.

Parameters

array $form: A form render array.

array $element_properties: The element's properties without hash prefix. Any property that is found in the form will be populated and unset from $element_properties array.

Return value

bool TRUE is the form has any inputs.

1 call to YamlFormElementBase::setConfigurationFormDefaultValueRecursive()
YamlFormElementBase::buildConfigurationForm in src/YamlFormElementBase.php
Form constructor.

File

src/YamlFormElementBase.php, line 1290

Class

YamlFormElementBase
Provides a base class for a form element.

Namespace

Drupal\yamlform

Code

protected function setConfigurationFormDefaultValueRecursive(array &$form, array &$element_properties) {
  $has_input = FALSE;
  foreach ($form as $property_name => &$property_element) {

    // Skip all properties.
    if (Element::property($property_name)) {
      continue;
    }

    // Skip Entity reference element 'selection_settings'.
    // @see \Drupal\yamlform\Plugin\YamlFormElement\YamlFormEntityReferenceTrait::form
    // @todo Fix entity reference AJAX and move code YamlFormEntityReferenceTrait.
    if (!empty($property_element['#tree']) && $property_name == 'selection_settings') {
      unset($element_properties[$property_name]);
      $property_element['#parents'] = [
        'properties',
        $property_name,
      ];
      $has_input = TRUE;
      continue;
    }

    // Determine if the property element is an input using the form element
    // manager.
    $is_input = $this->elementManager
      ->getElementInstance($property_element)
      ->isInput($property_element);
    if ($is_input) {
      if (isset($element_properties[$property_name])) {

        // If this property exists, then set its default value.
        $this
          ->setConfigurationFormDefaultValue($form, $element_properties, $property_element, $property_name);
        $has_input = TRUE;
      }
      else {

        // Else completely remove the property element from the form.
        unset($form[$property_name]);
      }
    }
    else {

      // Recurse down this container and see if it's children have inputs.
      // Note: #access is used to protect containers that should always
      // be visible.
      $container_has_input = $this
        ->setConfigurationFormDefaultValueRecursive($property_element, $element_properties);
      if ($container_has_input) {
        $has_input = TRUE;
      }
      elseif (empty($form[$property_name]['#access'])) {
        unset($form[$property_name]);
      }
    }
  }
  return $has_input;
}