0eb4b5a8a1d3fee95faea291856bd8433666b8ff
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d7 / hook / field_storage_load.twig
1 /**
2  * Implements hook_field_storage_load().
3  */
4 function {{ machine_name }}_field_storage_load($entity_type, $entities, $age, $fields, $options) {
5   $load_current = $age == FIELD_LOAD_CURRENT;
6
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);
15
16     $query = db_select($table, 't')
17       ->fields('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')
21       ->orderBy('delta');
22
23     if (empty($options['deleted'])) {
24       $query->condition('deleted', 0);
25     }
26
27     $results = $query->execute();
28
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;
33       }
34
35       if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) {
36         $item = array();
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;
42         }
43
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]++;
47       }
48     }
49   }
50 }