You are here

function multifield_field_load in Multifield 7

Same name and namespace in other branches
  1. 7.2 multifield.field.inc \multifield_field_load()

Implements hook_field_load().

File

./multifield.field.inc, line 84
Field integration for the Multifield module.

Code

function multifield_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
  $machine_name = multifield_extract_multifield_machine_name($field);
  foreach ($entities as $id => $entity) {
    array_walk($items[$id], 'multifield_item_unserialize', $machine_name);
  }
  foreach (multifield_type_get_subfields($machine_name) as $subfield_name) {
    $subfield = field_info_field($subfield_name);

    // Once the subfields have been imploded into a proper structure, we need
    // to filter out 'empty' values.
    foreach (array_keys($entities) as $id) {
      foreach ($items[$id] as $delta => $item) {
        if (!empty($item[$subfield_name][LANGUAGE_NONE])) {
          $items[$id][$delta][$subfield_name][LANGUAGE_NONE] = _field_filter_items($subfield, $item[$subfield_name][LANGUAGE_NONE]);
        }
      }
    }
  }
  $pseudo_entities = array();
  foreach ($entities as $id => $entity) {
    foreach ($items[$id] as $delta => $item) {
      $pseudo_entity = _multifield_field_item_to_entity($machine_name, $item);
      $pseudo_entity->delta = $delta;
      $pseudo_entity->parent_id = $id;
      $pseudo_entities[$pseudo_entity->id] = $pseudo_entity;
    }
  }

  // @todo Invoke hook_field_storage_pre_load()?
  // Invoke field-type module's hook_field_load().
  $null = NULL;
  $options = array();
  _field_invoke_multiple('load', 'multifield', $pseudo_entities, $age, $null, $options);

  // Invoke hook_field_attach_load(): let other modules act on loading the
  // entity.
  // @todo Should this move to getting invoked from multifield_field_attach_load()?
  module_invoke_all('field_attach_load', 'multifield', $pseudo_entities, $age, $options);

  // Store the loaded multifield entities in the entity cache.
  entity_get_controller('multifield')
    ->cacheSet($pseudo_entities);
  foreach ($pseudo_entities as $pseudo_entity) {
    $item =& $items[$pseudo_entity->parent_id][$pseudo_entity->delta];
    unset($pseudo_entity->delta);
    unset($pseudo_entity->parent_id);
    $item = _multifield_field_entity_to_item($pseudo_entity);
  }
}