You are here

function form_builder_set_element in Form Builder 6

Same name and namespace in other branches
  1. 7 includes/form_builder.api.inc \form_builder_set_element()

Recursive function to set an element within a form structure.

Return value

TRUE if an element was updated, FALSE if it was not found.

3 calls to form_builder_set_element()
form_builder_cache_field_save in includes/form_builder.cache.inc
Add or update a single field in a form builder cache.
form_builder_positions_submit in includes/form_builder.admin.inc
Submit handler for the form_builder_positions form.
form_builder_webform_save_node in modules/webform/form_builder_webform.module
Save the current Form Builder interface changes for a webform node.

File

includes/form_builder.api.inc, line 197
form_builder.api.inc Universally used API functions within the Form builder module.

Code

function form_builder_set_element(&$form, $element, &$entire_form = NULL, $parent_id = FORM_BUILDER_ROOT) {
  $return = FALSE;
  if (!isset($entire_form)) {
    $entire_form =& $form;
  }

  // Add new elements into the current parent.
  if (isset($element['#form_builder']['is_new']) && strcmp($element['#form_builder']['parent_id'], $parent_id) == 0) {
    unset($element['#form_builder']['is_new']);
    unset($element['#form_builder']['parent_id']);
    $new_key = $element['#key'];
    $form[$new_key] = $element;
    return TRUE;
  }
  foreach (element_children($form) as $key) {

    // Update an existing element if it lives in the current parent.
    if (isset($form[$key]['#form_builder']['element_id']) && $form[$key]['#form_builder']['element_id'] == $element['#form_builder']['element_id']) {

      // If the parent has changed, re-parent the element to a new fieldset.
      if (isset($element['#form_builder']['parent_id']) && strcmp($element['#form_builder']['parent_id'], $parent_id) != 0) {

        // Remove the current element from the form.
        unset($form[$key]);

        // Recurse again through the entire form to insert into the new position.
        $element['#form_builder']['is_new'] = TRUE;
        $return = form_builder_set_element($entire_form, $element);
      }
      elseif (isset($element['#key']) && $key != $element['#key']) {
        $new_key = $element['#key'];
        $index = array_search($key, array_keys($form));
        $before = array_slice($form, 0, $index, TRUE);
        $after = array_slice($form, $index + 1, NULL, TRUE);
        $form = $before + array(
          $new_key => $element,
        ) + $after;
        unset($form[$key]);
        $return = TRUE;
      }
      else {
        $form[$key] = $element;
        $return = TRUE;
      }
    }

    // Recurse into this element to look for the target element.
    if (!$return && isset($form[$key]['#form_builder'])) {
      $return = form_builder_set_element($form[$key], $element, $entire_form, $form[$key]['#form_builder']['element_id']);
    }
    if ($return) {
      return $return;
    }
  }
  return $return;
}