Same name and namespace in other branches
  1. 7.x includes/form.inc \_form_builder_handle_input_element()

Populate the #value and #name properties of input elements so they can be processed and rendered. Also, execute any #process handlers attached to a specific element.

Related topics

1 call to _form_builder_handle_input_element()
form_builder in includes/form.inc
Walk through the structured form array, adding any required properties to each element and mapping the incoming $_POST data to the proper elements.

File

includes/form.inc, line 1037

Code

function _form_builder_handle_input_element($form_id, &$form, &$form_state, $complete_form) {
  static $safe_core_value_callbacks = array(
    'form_type_token_value',
    'form_type_textfield_value',
    'form_type_checkbox_value',
    'form_type_checkboxes_value',
    'form_type_password_confirm_value',
    'form_type_select_value',
  );
  if (!isset($form['#name'])) {
    $name = array_shift($form['#parents']);
    $form['#name'] = $name;
    if ($form['#type'] == 'file') {

      // To make it easier to handle $_FILES in file.inc, we place all
      // file fields in the 'files' array. Also, we do not support
      // nested file names.
      $form['#name'] = 'files[' . $form['#name'] . ']';
    }
    elseif (count($form['#parents'])) {
      $form['#name'] .= '[' . implode('][', $form['#parents']) . ']';
    }
    array_unshift($form['#parents'], $name);
  }
  if (!isset($form['#id'])) {
    $form['#id'] = form_clean_id('edit-' . implode('-', $form['#parents']));
  }
  if (!empty($form['#disabled'])) {
    $form['#attributes']['disabled'] = 'disabled';
  }

  // With JavaScript or other easy hacking, input can be submitted even for
  // elements with #access=FALSE. For security, these must not be processed.
  // For pages with multiple forms, ensure that input is only processed for the
  // submitted form. drupal_execute() may bypass these checks and be treated as
  // a high privilege user submitting a single form.
  $process_input = $form['#programmed'] || (!isset($form['#access']) || $form['#access']) && isset($form['#post']) && (isset($form['#post']['form_id']) && $form['#post']['form_id'] == $form_id);
  if (!isset($form['#value']) && !array_key_exists('#value', $form)) {
    $function = !empty($form['#value_callback']) ? $form['#value_callback'] : 'form_type_' . $form['#type'] . '_value';
    if ($process_input) {
      $edit = $form['#post'];
      foreach ($form['#parents'] as $parent) {
        $edit = isset($edit[$parent]) ? $edit[$parent] : NULL;
      }
      if (!$form['#programmed'] || isset($edit)) {

        // Call #type_value to set the form value;
        if (function_exists($function)) {

          // Skip all value callbacks except safe ones like text if the CSRF
          // token was invalid.
          if (empty($form_state['invalid_token']) || in_array($function, $safe_core_value_callbacks)) {
            $form['#value'] = $function($form, $edit);
          }
          else {
            $edit = NULL;
          }
        }
        if (!isset($form['#value']) && isset($edit)) {
          $form['#value'] = $edit;
        }
      }

      // Mark all posted values for validation.
      if (isset($form['#value']) || isset($form['#required']) && $form['#required']) {
        $form['#needs_validation'] = TRUE;
      }
    }

    // Load defaults.
    if (!isset($form['#value'])) {

      // Call #type_value without a second argument to request default_value handling.
      if (function_exists($function)) {
        $form['#value'] = $function($form);
      }

      // Final catch. If we haven't set a value yet, use the explicit default value.
      // Avoid image buttons (which come with garbage value), so we only get value
      // for the button actually clicked.
      if (!isset($form['#value']) && empty($form['#has_garbage_value'])) {
        $form['#value'] = isset($form['#default_value']) ? $form['#default_value'] : '';
      }
    }
  }

  // Determine which button (if any) was clicked to submit the form.
  // We compare the incoming values with the buttons defined in the form,
  // and flag the one that matches. We have to do some funky tricks to
  // deal with Internet Explorer's handling of single-button forms, though.
  if ($process_input && !empty($form['#post']) && isset($form['#executes_submit_callback'])) {

    // First, accumulate a collection of buttons, divided into two bins:
    // those that execute full submit callbacks and those that only validate.
    $button_type = $form['#executes_submit_callback'] ? 'submit' : 'button';
    $form_state['buttons'][$button_type][] = $form;
    if (_form_button_was_clicked($form)) {
      $form_state['submitted'] = $form_state['submitted'] || $form['#executes_submit_callback'];

      // In most cases, we want to use form_set_value() to manipulate
      // the global variables. In this special case, we want to make sure that
      // the value of this element is listed in $form_variables under 'op'.
      $form_state['values'][$form['#name']] = $form['#value'];
      $form_state['clicked_button'] = $form;
      if (isset($form['#validate'])) {
        $form_state['validate_handlers'] = $form['#validate'];
      }
      if (isset($form['#submit'])) {
        $form_state['submit_handlers'] = $form['#submit'];
      }
    }
  }

  // Allow for elements to expand to multiple elements, e.g., radios,
  // checkboxes and files.
  if (isset($form['#process']) && !$form['#processed']) {
    foreach ($form['#process'] as $process) {
      if (function_exists($process)) {
        $form = $process($form, isset($edit) ? $edit : NULL, $form_state, $complete_form);
      }
    }
    $form['#processed'] = TRUE;
  }
  form_set_value($form, $form['#value'], $form_state);
}