You are here

protected function FormBase::addDefaults in Form Builder 7.2

Recurse through the form array and add defaults to their element arrays.

This function ensures the following properties: $element['#pre_render'] includes 'form_builder_pre_render' In $element['#form_builder']:

  • 'form_type'
  • 'form_id'
  • 'parent_id'
3 calls to FormBase::addDefaults()
FormBase::setElementArray in src/FormBase.php
(@inheritdoc}
FormBase::unserialize in src/FormBase.php
Unserialize a stored version of this form.
FormBase::__construct in src/FormBase.php

File

src/FormBase.php, line 147

Class

FormBase

Namespace

Drupal\form_builder

Code

protected function addDefaults(&$element, $parent_id = FORM_BUILDER_ROOT, $key = NULL, &$element_info = NULL) {
  if (!$element_info) {
    $element_info = Loader::instance()
      ->getElementTypeInfo($this->formType, $this->formId);
  }
  if (isset($element['#form_builder']['element_id'])) {
    $element_id = $element['#form_builder']['element_id'];
    $element += array(
      '#key' => $key,
    );
    $element['#form_builder']['form_type'] = $this->formType;
    $element['#form_builder']['form_id'] = $this->formId;
    $element['#form_builder']['parent_id'] = $parent_id;

    // Set defaults based on the form type.
    $settings = array();
    if (isset($element_info[$element_id]) && $element_info[$element_id]['unique']) {
      $element['#form_builder']['unique'] = TRUE;
      $element['#form_builder'] += array(
        'element_type' => $element_id,
      );
      $settings = $element_info[$element_id];
    }
    else {
      if (isset($element['#type'])) {
        $element['#form_builder'] += array(
          'element_type' => $element['#type'],
        );
      }
      if (isset($element_info[$element['#form_builder']['element_type']])) {
        $settings = $element_info[$element['#form_builder']['element_type']];
      }
      else {

        // If the type cannot be found, prevent editing of this field.
        unset($element['#form_builder']);
        return;
      }
    }

    // Set defaults for configurable and removable.
    $settings += array(
      'configurable' => TRUE,
      'removable' => TRUE,
    );
    $element['#form_builder'] += array(
      'configurable' => $settings['configurable'],
      'removable' => $settings['removable'],
    );
    $parent_id = $element_id;
  }
  foreach (element_children($element) as $key) {
    $this
      ->addDefaults($element[$key], $parent_id, $key, $element_info);
  }
}