You are here

function text_field in Content Construction Kit (CCK) 6

Same name in this branch
  1. 6 examples/simple_field.php \text_field()
  2. 6 examples/example_field.php \text_field()
  3. 6 modules/text/text.module \text_field()
Same name and namespace in other branches
  1. 5 text.module \text_field()
  2. 6.3 modules/text/text.module \text_field()
  3. 6.2 modules/text/text.module \text_field()

Implementation of hook_field().

Define the behavior of a field type.

Parameters

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

  • "load": The node is about to be loaded from the database. This hook should be used to load the field.
  • "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().
  • "presave": The user has just finished editing the node and the node has passed validation. This hook can be used to modify the node.
  • "insert": The node is being created (inserted in the database).
  • "update": The node is being updated.
  • "delete": The node is being deleted.

&$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 "load" operation should return an object containing extra values to be merged into the node object.
  • The "insert", "update", "delete", "validate", and "presave" operations have no return value.

In most cases, only "validate" operations is relevant ; the rest have default implementations in content_field() that usually suffice.

File

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

Code

function text_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'validate':
      $allowed_values = content_allowed_values($field);
      if (is_array($items)) {
        foreach ($items as $delta => $item) {
          $error_field = $field['field_name'] . '][' . $delta . '][value';
          if ($item['value'] != '') {
            if (count($allowed_values) && !array_key_exists($item['value'], $allowed_values)) {
              form_set_error($error_field, t('Illegal value for %name.', array(
                '%name' => t($field['widget']['label']),
              )));
            }
          }
        }
      }
      if (!empty($field['max_length'])) {
        foreach ($items as $delta => $data) {
          $error_field = $field['field_name'] . '][' . $delta . '][value';
          if (strlen($data['value']) > $field['max_length']) {
            form_set_error($error_field, t('%label is longer than %max characters.', array(
              '%label' => $field['widget']['label'],
              '%max' => $field['max_length'],
            )));
          }
        }
      }
      break;
  }
}