function hook_field_load

You are here

7 field.api.php hook_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age)

Define custom load behavior for this module's field types.

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 entity should be loaded in a single query where possible.

Note that the changes made to the field values get cached by the field cache for subsequent loads. You should never use this hook to load fieldable entities, since this is likely to cause infinite recursions when hook_field_load() is run on those as well. Use hook_field_formatter_prepare_view() instead.

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

Parameters

$entity_type: The type of $entity.

$entities: Array of entities being loaded, 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 code associated with $items.

$items: Array of field values already loaded for the entities, keyed by entity ID. Store your changes in this parameter (passed by reference).

$age: FIELD_LOAD_CURRENT to load the most recent revision for all fields, or FIELD_LOAD_REVISION to load the version indicated by each entity.

Related topics

4 functions implement hook_field_load()

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

field_test_field_load in modules/field/tests/field_test.field.inc
Implements hook_field_load().
file_field_load in modules/file/file.field.inc
Implements hook_field_load().
image_field_load in modules/image/image.field.inc
Implements hook_field_load().
text_field_load in modules/field/modules/text/text.module
Implements hook_field_load().
2 invocations of hook_field_load()
field_attach_load in modules/field/field.attach.inc
Loads fields for the current revisions of a group of entities.
node_preview in modules/node/node.pages.inc
Generates a node preview.

File

modules/field/field.api.php, line 308

Code

function hook_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
  // Sample code from text.module: precompute sanitized strings so they are
  // stored in the field cache.
  foreach ($entities as $id => $entity) {
    foreach ($items[$id] as $delta => $item) {
      // Only process items with a cacheable format, the rest will be handled
      // by formatters if needed.
      if (empty($instances[$id]['settings']['text_processing']) || filter_format_allowcache($item['format'])) {
        $items[$id][$delta]['safe_value'] = isset($item['value']) ? _text_sanitize($instances[$id], $langcode, $item, 'value') : '';
        if ($field['type'] == 'text_with_summary') {
          $items[$id][$delta]['safe_summary'] = isset($item['summary']) ? _text_sanitize($instances[$id], $langcode, $item, 'summary') : '';
        }
      }
    }
  }
}

Comments

I wrote this helper function that will return field data for a given entity id. This saves on having to do a full entity load for one field's data.

/**
* Helper that returns data for a single entity field.
*/
function _otsit012_single_entity_field_load($entity_type, $field_name, $entity_id, $current = TRUE) {

  $empty_entity_object = new stdClass();
  $entities = array($entity_id => $empty_entity_object);

  $age = $current ? FIELD_LOAD_CURRENT : FALSE;

  $field_system_id_result = db_query("SELECT id FROM {field_config} WHERE field_name = :field_name",
      array(':field_name' => $field_name))->fetch();
  $fields = array($field_system_id_result->id => array($entity_id));

  $options = array();

  field_sql_storage_field_storage_load($entity_type, $entities, $age, $fields, $options);

  return $entities[$entity_id];
}