2 * Implements hook_field_storage_load().
4 function {{ machine_name }}_field_storage_load($entity_type, $entities, $age, $fields, $options) {
5 $load_current = $age == FIELD_LOAD_CURRENT;
7 foreach ($fields as $field_id => $ids) {
8 // By the time this hook runs, the relevant field definitions have been
9 // populated and cached in FieldInfo, so calling field_info_field_by_id()
10 // on each field individually is more efficient than loading all fields in
11 // memory upfront with field_info_field_by_ids().
12 $field = field_info_field_by_id($field_id);
13 $field_name = $field['field_name'];
14 $table = $load_current ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field);
16 $query = db_select($table, 't')
18 ->condition('entity_type', $entity_type)
19 ->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN')
20 ->condition('language', field_available_languages($entity_type, $field), 'IN')
23 if (empty($options['deleted'])) {
24 $query->condition('deleted', 0);
27 $results = $query->execute();
29 $delta_count = array();
30 foreach ($results as $row) {
31 if (!isset($delta_count[$row->entity_id][$row->language])) {
32 $delta_count[$row->entity_id][$row->language] = 0;
35 if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) {
37 // For each column declared by the field, populate the item
38 // from the prefixed database column.
39 foreach ($field['columns'] as $column => $attributes) {
40 $column_name = _field_sql_storage_columnname($field_name, $column);
41 $item[$column] = $row->$column_name;
44 // Add the item to the field values for the entity.
45 $entities[$row->entity_id]->{$field_name}[$row->language][] = $item;
46 $delta_count[$row->entity_id][$row->language]++;