field_default_form

Versions
7
field_default_form($obj_type, $object, $field, $instance, $langcode, $items, &$form, &$form_state, $get_delta = NULL)

Create a separate form element for each field.

Code

modules/field/field.form.inc, line 11

<?php
function field_default_form($obj_type, $object, $field, $instance, $langcode, $items, &$form, &$form_state, $get_delta = NULL) {
  // This could be called with no object, as when a UI module creates a
  // dummy form to set default values.
  if ($object) {
    list($id, , ) = entity_extract_ids($obj_type, $object);
  }

  $field_name = $field['field_name'];

  // Put field information at the top of the form, so that it can be easily
  // retrieved.
  // Note : widgets and other form handling code should *always* fetch field
  // and instance information from $form['#fields'] rather than from
  // field_info_field(). This lets us build forms for 'variants' of a field,
  // for instance on admin screens.
  $form['#fields'][$field_name] = array(
    'field' => $field,
    'instance' => $instance,
  );

  // Populate widgets with default values when creating a new object.
  if (empty($items) && empty($id)) {
    $items = field_get_default_value($obj_type, $object, $field, $instance, $langcode);
  }

  $field_elements = array();

  if (field_access('edit', $field, $obj_type, $object)) {
    // If field module handles multiple values for this form element, and we
    // are displaying an individual element, process the multiple value form.
    if (!isset($get_delta) && field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_DEFAULT) {
      $field_elements = field_multiple_value_form($field, $instance, $langcode, $items, $form, $form_state);
    }
    // If the widget is handling multiple values (e.g Options), or if we are
    // displaying an individual element, just get a single form element and
    // make it the $delta value.
    else {
      $delta = isset($get_delta) ? $get_delta : 0;
      $function = $instance['widget']['module'] . '_field_widget';
      if (function_exists($function)) {
        $element = array(
          '#object_type' => $instance['object_type'],
          '#bundle' => $instance['bundle'],
          '#field_name' => $field_name,
          '#columns' => array_keys($field['columns']),
          '#title' => check_plain(t($instance['label'])),
          '#description' => field_filter_xss($instance['description']),
          // Only the first widget should be required.
          '#required' => $delta == 0 && $instance['required'],
          '#delta' => $delta,
        );
        if ($element = $function($form, $form_state, $field, $instance, $langcode, $items, $delta, $element)) {
          // If we're processing a specific delta value for a field where the
          // field module handles multiples, set the delta in the result.
          // For fields that handle their own processing, we can't make
          // assumptions about how the field is structured, just merge in the
          // returned element.
          if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_DEFAULT) {
            $field_elements[$delta] = $element;
          }
          else {
            $field_elements = $element;
          }
        }
      }
    }
  }

  if ($field_elements) {
    // Add the field form element as a child keyed by language code to match
    // the field data structure:
    // $object->{$field_name}[$langcode][$delta][$column].
    // The '#language' key can be used to access the field's form element
    // when $langcode is unknown. The #weight property is inherited from the
    // field's form element.
    // Also aid in theming of field widgets by rendering a classified
    // container.
    $addition[$field_name] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'field-type-' . drupal_html_class($field['type']),
          'field-name-' . drupal_html_class($field_name),
          'field-widget-' . drupal_html_class($instance['widget']['type']),
        ),
      ),
      '#tree' => TRUE,
      '#weight' => $instance['widget']['weight'],
      '#language' => $langcode,
      $langcode => $field_elements,
    );
  }
  else {
    // The field is not accessible, or the widget did not return anything. Make
    // sure the items are available in the submitted form values.
    foreach ($items as $delta => $item) {
      $field_elements[$delta] = array(
        '#type' => 'value',
        '#value' => $item,
      );
    }
    $addition[$field_name] = array(
      '#tree' => TRUE,
      '#language' => $langcode,
      $langcode => $field_elements,
    );
  }

  $form['#fields'][$field_name]['form_path'] = array($field_name);

  return $addition;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.