field_format
- Versions
- 7
field_format($obj_type, $object, $field, $item, $formatter_type = NULL, $formatter_settings = array())
Format a field item for display.
TODO D7 : do we still need field_format ?
- backwards compatibility of templates - check what fallbacks we can propose...
- was used by Views integration in CCK in D6 - do we need now?
At least needs a little rehaul/update...
Used to display a field's values outside the context of the $node, as when fields are displayed in Views, or to display a field in a template using a different formatter than the one set up on the 'Manage display' tab for the node's context.
Parameters
$field Either a field array or the name of the field.
$item The field item(s) to be formatted (such as $node->field_foo[0], or $node->field_foo if the formatter handles multiple values itself)
$formatter_type The name of the formatter type to use.
$node Optionally, the containing node object for context purposes and field-instance options.
Return value
A string containing the contents of the field item(s) sanitized for display. It will have been passed through the necessary check_plain() or check_markup() functions as necessary.
Related topics
Code
modules/field/field.module, line 502
<?php
function field_format($obj_type, $object, $field, $item, $formatter_type = NULL, $formatter_settings = array()) {
if (!is_array($field)) {
$field = field_info_field($field);
}
if (field_access('view', $field, $obj_type, $object)) {
$field_type = field_info_field_types($field['type']);
// We need $field, $instance, $obj_type, $object to be able to display a value...
list(, , $bundle) = entity_extract_ids($obj_type, $object);
$instance = field_info_instance($obj_type, $field['field_name'], $bundle);
$display = array(
'type' => $formatter_type ? $formatter_type : $field_type['default_formatter'],
'settings' => $formatter_settings,
);
$display['settings'] += field_info_formatter_settings($display['type']);
if ($display['type'] !== 'hidden') {
$theme = $formatter['module'] . '_formatter_' . $display['type'];
$element = array(
'#theme' => $theme,
'#field_name' => $field['field_name'],
'#object_type' => $obj_type,
'#bundle' => $bundle,
'#formatter' => $display['type'],
'#settings' => $display['settings'],
'#object' => $object,
'#object_type' => $obj_type,
'#delta' => isset($item['#delta']) ? $item['#delta'] : NULL,
);
if (field_behaviors_formatter('multiple values', $display) == FIELD_BEHAVIOR_DEFAULT) {
// Single value formatter.
// hook_field('sanitize') expects an array of items, so we build one.
$items = array($item);
$function = $field['module'] . '_field_sanitize';
if (function_exists($function)) {
$function($obj_type, $object, $field, $instance, $items);
}
$element['#item'] = $items[0];
}
else {
// Multiple values formatter.
$items = $item;
$function = $field['module'] . '_field_sanitize';
if (function_exists($function)) {
$function($obj_type, $object, $field, $instance, $items);
}
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#item' => $item,
'#weight' => $delta,
);
}
}
return theme($theme, $element);
}
}
}
?>Login or register to post comments 