You are here

function hook_widget in Content Construction Kit (CCK) 5

Define the behavior of a widget.

Parameters

$op: What kind of action is being performed. Possible values:

  • "prepare form values": The editing form will be displayed. The widget should perform any conversion necessary from the field's native storage format into the storage used for the form. Convention dictates that the widget's version of the data should be stored beginning with "default".
  • "form": The node is being edited, and a form should be prepared for display to the user.
  • "validate": The user has just finished editing the node and is trying to preview or submit it. This hook can be used to check or even modify the node. Errors should be set with form_set_error().
  • "process form values": The inverse of the prepare operation. The widget should convert the data back to the field's native format.
  • "submit": The user has just finished editing the node and the node has passed validation. This hook can be used to modify the node.

&$node: The node the action is being performed on. This argument is passed by reference for performance only; do not modify it.

$field: The field the action is being performed on.

&$node_field: The contents of the field in this node. Changes to this variable will be saved back to the node object.

Return value

This varies depending on the operation.

  • The "form" operation should return an array of form elements to display.
  • Other operations have no return value.
5 functions implement hook_widget()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

nodereference_widget in ./nodereference.module
Implementation of hook_widget().
number_widget in ./number.module
Implementation of hook_widget().
optionwidgets_widget in ./optionwidgets.module
Implementation of hook_widget().
text_widget in ./text.module
Implementation of hook_widget().
userreference_widget in ./userreference.module
Implementation of hook_widget().

File

./field.php, line 410
These hooks are defined by field modules, modules that define a new kind of field for insertion in a content type.

Code

function hook_widget($op, &$node, $field, &$node_field) {
  switch ($op) {
    case 'prepare form values':
      if ($field['multiple']) {
        $node_field_transposed = content_transpose_array_rows_cols($node_field);
        $node_field['default nids'] = $node_field_transposed['nid'];
      }
      else {
        $node_field['default nids'] = array(
          $node_field['nid'],
        );
      }
      break;
    case 'form':
      $form = array();
      $form[$field['field_name']] = array(
        '#tree' => TRUE,
      );
      $form[$field['field_name']]['nids'] = array(
        '#type' => 'select',
        '#title' => t($field['widget']['label']),
        '#default_value' => $node_field['default nids'],
        '#multiple' => $field['multiple'],
        '#options' => _nodereference_potential_references($field),
        '#required' => $field['required'],
        '#description' => $field['widget']['description'],
      );
      return $form;
    case 'process form values':
      if ($field['multiple']) {
        $node_field = content_transpose_array_rows_cols(array(
          'nid' => $node_field['nids'],
        ));
      }
      else {
        $node_field['nid'] = is_array($node_field['nids']) ? reset($node_field['nids']) : $node_field['nids'];
      }
      break;
  }
}