function hook_field in Content Construction Kit (CCK) 5
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.
- "view": The node is about to be presented to the user. The module should prepare and return an HTML string containing a default representation of the field. It will be called only if 'view' was set to TRUE in hook_field_settings('callbacks')
- "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().
- "submit": 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 "view" operation should return a string containing an HTML representation of the field data.
- The "insert", "update", "delete", "validate", and "submit" operations have no return value.
In most cases, only "validate" operations is relevant ; the rest have default implementations in content_field() that usually suffice.
11 functions implement hook_field()
Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.
- content_alter_db_field in ./
content_admin.inc - Perform adds, alters, and drops as needed to synchronize the database with new field definitions.
- content_field in ./
content.module - Implementation of hook_field(). Handles common field housekeeping.
- content_panels_edit_field in ./
content_panels.inc - content_panels_render_field in ./
content_panels.inc - nodereference_field in ./
nodereference.module - Implementation of hook_field().
File
- ./
field.php, line 200 - These hooks are defined by field modules, modules that define a new kind of field for insertion in a content type.
Code
function hook_field($op, &$node, $field, &$node_field, $teaser, $page) {
switch ($op) {
case 'view':
$context = $teaser ? 'teaser' : 'full';
$formatter = isset($field['display_settings'][$context]['format']) ? $field['display_settings'][$context]['format'] : 'default';
$items = array();
foreach ($node_field as $delta => $item) {
$items[$delta]['view'] = content_format($field, $item, $formatter, $node);
}
return theme('field', $node, $field, $items, $teaser, $page);
case 'validate':
$allowed_values = text_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']),
)));
}
}
}
}
break;
}
}