field_attach_form

Versions
7
field_attach_form($obj_type, $object, &$form, &$form_state, $langcode = NULL)

Add form elements for all fields for an object to a form structure.

// One sub-array per field appearing in the form, keyed by field name. // The structure of the array differs slightly depending on whether the // widget is 'single-value' (provides the input for one field value, // most common case), and will therefore be repeated as many times as // needed, or 'multiple-values' (one single widget allows the input of // several values, e.g checkboxes, select box...). // The sub-array is nested into a $langcode key where $langcode has the // same value of the $langcode parameter above. This allow us to match // the field data structure ($field_name[$langcode][$delta][$column]). // The '#language' key holds the same value of $langcode and it is used // to access the field sub-array when $langcode is unknown. 'field_foo' => array( '#tree' => TRUE, '#language' => $langcode, $langcode => array( '#field_name' => the name of the field, '#tree' => TRUE, '#required' => whether or not the field is required, '#title' => the label of the field instance, '#description' => the description text for the field instance,

// Only for 'single' widgets: '#theme' => 'field_multiple_value_form', '#cardinality' => the field cardinality, // One sub-array per copy of the widget, keyed by delta. 0 => array( '#title' => the title to be displayed by the widget, '#default_value' => the field value for delta 0, '#required' => whether the widget should be marked required, '#delta' => 0, '#field_name' => the name of the field, '#bundle' => the name of the bundle, '#columns' => the array of field columns, // The remaining elements in the sub-array depend on the widget. '#type' => the type of the widget, ... ), 1 => array( ... ),

// Only for multiple widgets: '#bundle' => $instance['bundle'], '#columns' => array_keys($field['columns']), // The remaining elements in the sub-array depend on the widget. '#type' => the type of the widget, ... ), ... ), ) @endcode

Parameters

$obj_type The type of $object; e.g. 'node' or 'user'.

$object The object for which to load form elements, used to initialize default form values.

$form The form structure to fill in.

$form_state An associative array containing the current state of the form.

$langcode The language the field values are going to be entered, if no language is provided the default site language will be used.

Return value

The form elements are added by reference at the top level of the $form parameter. Sample structure: @code array( '#fields' => array( // One sub-array per field appearing in the form, keyed by field name. 'field_foo' => array ( 'field' => the field definition structure, 'instance' => the field instance definition structure, 'form_path' => an array of keys indicating the path to the field element within the full $form structure, used by the 'add more values' AHAH button. Any 3rd party module using form_alter() to modify the structure of the form should update this entry as well. ), ),

Related topics

▾ 6 functions call field_attach_form()

comment_form in modules/comment/comment.module
Generate the basic commenting form, for appending to a node or display on a separate page.
field_test_entity_form in modules/field/tests/field_test.entity.inc
Test_entity form.
node_form in modules/node/node.pages.inc
Generate the node add/edit form array.
taxonomy_form_term in modules/taxonomy/taxonomy.admin.inc
Form function for the term edit form.
user_profile_form in modules/user/user.pages.inc
Form builder; edit a user account or one of their profile categories.
user_save in modules/user/user.module
Save changes to a user account or add a new user.

Code

modules/field/field.attach.inc, line 484

<?php
function field_attach_form($obj_type, $object, &$form, &$form_state, $langcode = NULL) {
  // If no language is provided use the default site language.
  $options = array('language' => field_multilingual_valid_language($langcode));
  $form += (array) _field_invoke_default('form', $obj_type, $object, $form, $form_state, $options);

  // Add custom weight handling.
  list($id, $vid, $bundle) = entity_extract_ids($obj_type, $object);
  $form['#attached']['css'][] = drupal_get_path('module', 'field') . '/theme/field.css';
  $form['#pre_render'][] = '_field_extra_weights_pre_render';
  $form['#extra_fields'] = field_extra_fields($bundle);

  // Let other modules make changes to the form.
  // Avoid module_invoke_all() to let parameters be taken by reference.
  foreach (module_implements('field_attach_form') as $module) {
    $function = $module . '_field_attach_form';
    $function($obj_type, $object, $form, $form_state, $langcode);
  }
}
?>
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.