You are here

function content_field_form in Content Construction Kit (CCK) 6.3

Same name and namespace in other branches
  1. 6 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.

5 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_field_edit_form in includes/content.admin.inc
Menu callback; presents the field editing page.
content_form in includes/content.node_form.inc
@file Create fields' form for a content type.
content_multigroup_group_form in modules/content_multigroup/content_multigroup.node_form.inc
Create a new delta value for the group.
content_rules_action_populate_field_form in includes/content.rules.inc
Action "populate a field" configuration form. This is a multistep form!

File

includes/content.node_form.inc, line 37
Create fields' form for a content type.

Code

function content_field_form(&$form, &$form_state, $field, $get_delta = NULL) {
  $form['#cache'] = FALSE;
  $node = $form['#node'];
  $addition = array();
  $form_element = array();
  $field_name = $field['field_name'];
  $items = 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';
  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).
    if (!empty($form_state['values'][$field['field_name']])) {
      $items = $form_state['values'][$field['field_name']];

      // If there was an AHAH add more button in this field, don't save it.
      unset($items[$field['field_name'] . '_add_more']);
    }
    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);
        }
      }
    }

    // See if access to this form element is restricted,
    // if so, skip widget processing and just set the value.
    $access = content_access('edit', $field, NULL, $node);
    if (!$access) {
      $addition[$field_name] = array(
        '#access' => $access,
        '#type' => 'value',
        '#value' => $items,
      );
      return $addition;
    }

    // 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)) {
        $title = check_plain(t($field['widget']['label']));
        $description = content_filter_xss(t($field['widget']['description']));
        $defaults = array(
          '#required' => $get_delta > 0 ? FALSE : $field['required'],
          '#columns' => array_keys($field['columns']),
          '#title' => $title,
          '#description' => $description,
          '#delta' => $delta,
          '#field_name' => $field['field_name'],
          '#type_name' => $field['type_name'],
        );

        // 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'],
        '#access' => $access,
        // 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;
}