field_default_form

Versions
mediamosa-21
field_default_form($entity_type, $entity, $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($entity_type, $entity, $field, $instance, $langcode, $items, &$form, &$form_state, $get_delta = NULL) {
  // This could be called with no entity, as when a UI module creates a
  // dummy form to set default values.
  if ($entity) {
    list($id, , ) = entity_extract_ids($entity_type, $entity);
  }

  $field_name = $field['field_name'];
  $addition[$field_name] = array();

  // Store field information in $form_state['storage'].
  $form_state['field'][$field_name][$langcode] = array(
    'field' => $field,
    'instance' => $instance,
    // This entry will be populated at form build time.
    'array_parents' => array(),
    // This entry will be populated at form validation time.
    'errors' => array(),
  );

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

  // Collect widget elements.
  $elements = array();
  if (field_access('edit', $field, $entity_type, $entity)) {
    // 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) {
      $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_form';
      if (function_exists($function)) {
        $element = array(
          '#object_type' => $instance['object_type'],
          '#bundle' => $instance['bundle'],
          '#field_name' => $field_name,
          '#language' => $langcode,
          '#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) {
            $elements[$delta] = $element;
          }
          else {
            $elements = $element;
          }
        }
      }
    }
  }

  if ($elements) {
    // 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']),
        ),
      ),
      '#weight' => $instance['widget']['weight'],
    );
  }
  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) {
      $elements[$delta] = array(
        '#type' => 'value',
        '#value' => $item,
      );
    }
  }

  // Populate the 'array_parents' information in $form_state['field'] after
  // the form is built, so that we catch changes in the form structure performed
  // in alter() hooks.
  $elements['#after_build'][] = 'field_form_element_after_build';
  $elements['#field_name'] = $field_name;
  $elements['#language'] = $langcode;

  $addition[$field_name] += array(
    '#tree' => TRUE,
    // The '#language' key can be used to access the field's form element
    // when $langcode is unknown.
    '#language' => $langcode,
    $langcode => $elements,
  );

  return $addition;
}
?>