You are here

function content_field_form in Content Construction Kit (CCK) 6

Same name and namespace in other branches
  1. 6.3 includes/content.node_form.inc \content_field_form()
  2. 6.2 includes/content.node_form.inc \content_field_form()

Create a separate form element for each field.

// TODO : $count param ? not used anymore ? Hook_widget() picks up two new values, $count and $delta, to help widgets know what information to return since multiple values are sometimes controlled by the content module.

Parameters

$form: the form to add this field element to

$form_state: the form_state for the above form

$field: the field array to use to create the form element

$get_delta: use to get only a specific delta value of a multiple value field, otherwise function will return the entire $field form element.

3 calls to content_field_form()
content_add_more_js in includes/content.node_form.inc
Menu callback for AHAH addition of new empty widgets.
content_form in includes/content.node_form.inc
Create fields' form for a content type.
_content_admin_field in includes/content.admin.inc
Menu callback; presents the field editing page.

File

includes/content.node_form.inc, line 36

Code

function content_field_form(&$form, &$form_state, $field, $get_delta = NULL) {
  $form['#cache'] = FALSE;
  $node = $form['#node'];
  $addition = array();
  $form_element = array();

  // TODO : is the "if (function_exists($function)) {" needed ?
  // defining the $function here makes it unclear where it is actually called
  $function = $field['widget']['module'] . '_widget';
  $field_name = $field['field_name'];
  if (function_exists($function)) {

    // Prepare the values to be filled in the widget.
    // We look in the following places :
    // - Form submitted values
    // - Node values (when editing an existing node), or pre-filled values (when
    //   creating a new node translation)
    // - Default values set for the field (when creating a new node).
    $items = array();
    if (!empty($form_state['values'][$field['field_name']])) {
      $items = $form_state['values'][$field['field_name']];
    }
    elseif (!empty($node->{$field}['field_name'])) {
      $items = $node->{$field}['field_name'];
    }
    elseif (empty($node->nid)) {
      if (content_callback('widget', 'default value', $field) != CONTENT_CALLBACK_NONE) {

        // If a module wants to insert custom default values here,
        // it should provide a hook_default_value() function to call,
        // otherwise the content module's content_default_value() function
        // will be used.
        $callback = content_callback('widget', 'default value', $field) == CONTENT_CALLBACK_CUSTOM ? $field['widget']['module'] . '_default_value' : 'content_default_value';
        if (function_exists($callback)) {
          $items = $callback($form, $form_state, $field, 0);
        }
      }
    }

    // If content module handles multiple values for this form element,
    // and not selecting an individual $delta, process the multiple value form.
    if (!isset($get_delta) && content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE) {
      $form_element = content_multiple_value_form($form, $form_state, $field, $items);
    }
    else {
      $delta = isset($get_delta) ? $get_delta : 0;
      if ($element = $function($form, $form_state, $field, $items, $delta)) {
        $defaults = array(
          '#field_name' => $field['field_name'],
          '#required' => $get_delta > 0 ? FALSE : $field['required'],
          // TODO : should we add some logic for title and description too ?
          '#columns' => array_keys($field['columns']),
          '#title' => t($field['widget']['label']),
          '#delta' => $delta,
        );

        // If we're processing a specific delta value for a field where the
        // content 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 value.
        if (content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE) {
          $form_element[$delta] = array_merge($element, $defaults);
        }
        else {
          $form_element = array_merge($element, $defaults);
        }
      }
    }

    // Field name is needed at top level as well as the individual elements
    // so the multiple values or other field level theme or processing can find it.
    if ($form_element) {
      $defaults = array(
        '#field_name' => $field['field_name'],
        '#tree' => TRUE,
        '#weight' => $field['widget']['weight'],
        // TODO : what's the need for #count ? does not seem to be used anywhere ?
        '#count' => count($form_element),
      );
      $addition[$field['field_name']] = array_merge($form_element, $defaults);
    }
  }
  return $addition;
}