You are here

protected function FormBuilderFormBase::addDefaults in Form Builder 7

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 FormBuilderFormBase::addDefaults()
FormBuilderFormBase::setElementArray in ./form_builder.classes.inc
(@inheritdoc}
FormBuilderFormBase::unserialize in ./form_builder.classes.inc
Unserialize a stored version of this form.
FormBuilderFormBase::__construct in ./form_builder.classes.inc

File

./form_builder.classes.inc, line 465

Class

FormBuilderFormBase

Code

protected function addDefaults(&$element, $parent_id = FORM_BUILDER_ROOT, $key = NULL, &$element_info = NULL) {
  if (!$element_info) {
    $element_info = FormBuilderLoader::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);
  }
}