You are here

public function FormBase::setElementArray in Form Builder 7.2

(@inheritdoc}

1 call to FormBase::setElementArray()
Form::addComponents in modules/webform/src/Form.php
Add components to the form.

File

src/FormBase.php, line 265

Class

FormBase

Namespace

Drupal\form_builder

Code

public function setElementArray($element, $parent_id = FORM_BUILDER_ROOT, $alter = FALSE) {
  $return = FALSE;
  $element_id = $element['#form_builder']['element_id'];
  $element['#form_builder'] += array(
    'parent_id' => $parent_id,
  );
  $parent_id = $element['#form_builder']['parent_id'];
  if ($alter) {
    drupal_alter('form_builder_add_element', $element, $this->formType, $this->formId);

    // Save any element ID set by the hook_form_builder_add_element_alter().
    $element_id = $element['#form_builder']['element_id'];
    $parent_id = $element['#form_builder']['parent_id'];

    // Re-run addDefaults in case something has changed
  }
  $this
    ->addDefaults($element, $parent_id);
  if (!isset($element['#form_builder'])) {
    return FALSE;
  }
  if (isset($this->elementArrays[$parent_id])) {
    $parent =& $this->elementArrays[$parent_id];
  }
  else {
    return FALSE;
  }
  $old_element = FALSE;
  if (isset($this->elementArrays[$element_id])) {
    $old_element =& $this->elementArrays[$element_id];

    // Remove element from old parent if needed.
    if ($parent_id !== $old_element['#form_builder']['parent_id']) {
      $old_parent =& $this->elementArrays[$old_element['#form_builder']['parent_id']];
      unset($old_parent[$old_element['#key']]);
      unset($old_element);
      unset($old_parent);
      $old_element = FALSE;
    }
  }
  if ($old_element && $old_element['#key'] != $element['#key']) {

    // Insert the (changed) element at the same position in the parent.
    $new_parent = array();
    foreach ($parent as $key => &$child) {
      if ($key == $old_element['#key']) {
        $new_parent[$element['#key']] =& $element;
      }
      else {
        $new_parent[$key] =& $child;
      }
    }
    $parent = $new_parent;
  }
  else {
    $parent[$element['#key']] =& $element;
  }
  $this
    ->indexElements($element);
  return $element_id;
}