7.x field.api.php hook_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items)

Prepare field values prior to display.

This hook is invoked before the field values are handed to formatters for display, and runs before the formatters' own hook_field_formatter_prepare_view().

Unlike most other field hooks, this hook operates on multiple entities. The $entities, $instances and $items parameters are arrays keyed by entity ID. For performance reasons, information for all available entities should be loaded in a single query where possible.

Make changes or additions to field values by altering the $items parameter by reference. There is no return value.


$entity_type: The type of $entity.

$entities: Array of entities being displayed, keyed by entity ID.

$field: The field structure for the operation.

$instances: Array of instance structures for $field for each entity, keyed by entity ID.

$langcode: The language associated to $items.

$items: $entity->{$field['field_name']}, or an empty array if unset.

Related topics

2 functions implement hook_field_prepare_view()

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

file_field_prepare_view in modules/file/file.field.inc
Implements hook_field_prepare_view().
image_field_prepare_view in modules/image/image.field.inc
Implements hook_field_prepare_view().
2 invocations of hook_field_prepare_view()
field_attach_prepare_view in modules/field/field.attach.inc
Prepare field data prior to display.
field_view_field in modules/field/field.module
Returns a renderable array for the value of a single field in an entity.


modules/field/field.api.php, line 362
Hooks provided by the Field module.


function hook_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {

  // Sample code from image.module: if there are no images specified at all,
  // use the default image.
  foreach ($entities as $id => $entity) {
    if (empty($items[$id]) && $field['settings']['default_image']) {
      if ($file = file_load($field['settings']['default_image'])) {
        $items[$id][0] = (array) $file + array(
          'is_default' => TRUE,
          'alt' => '',
          'title' => '',


juampynr’s picture

If you define a field which only renders data but does not store anything in the database, you may find out that it is never printed since the Field API considers that it is empty. Therefore, the $items array will be always with a size of 0.

You can overcome this by adding a dummy value to the $items array by implementing this hook:

 * Implements hook_field_prepare_view().
 * Adds a dummy value to the field to make rendering possible.
function mymodule_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
  if ($field['type'] == 'mymodule_myfield') {
    foreach ($items as $key => $item) {
      if (!isset($item[0]['value'])) {
        $items[$key][0]['value'] = 'Dummy value';
martin_q’s picture

You are right - this fixed my problem of no output from my field! I've referenced your comment in the documentation for hook_field_formatter_view here to help anyone else who is as puzzled as I was.

DaPooch’s picture

For some reason I can't get my module to call this hook. hook_field_formatter_prepare_view worked but not this one. Not sure why.

texas-bronius’s picture

Thought this was unexpected, too. I followed the thread here to provide a default (empty) value to allow my custom field formatter to display when no value is provided ($items[0] DNE), and while this specified field prepare view did not work for me, hook_field_formatter_prepare_view did. Maybe it's because it's a completely custom field?

texas-bronius’s picture

Maybe this hook is not called in my case because I am not specifying anything but a field formatter (view only), not a field edit.

ivanzhu’s picture

I have the same problem with you. After I read the comment about hook_field_formatter_prepare_view and have a testing. I think your opinion is right.

smurfxx’s picture

Can I alter the number of values of a field with this function? I'd like to dynamically change it.