function field_collection_entity_preload in Field collection 7
1 call to field_collection_entity_preload()
File
- ./
field_collection.module, line 967 - Module implementing field collection field type.
Code
function field_collection_entity_preload($entities, $langcode, &$items, $fields) {
static $local_entity_cache = array();
$fc_ids = array();
$new_items = array();
// Collect every possible term attached to any of the fieldable entities.
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
// Check if the entity already exists.
if (isset($item['entity'])) {
continue;
}
// Check if this item is in our local entity cache.
if (isset($local_entity_cache[$item['value']])) {
$items[$id][$delta]['field_collection'] = $local_entity_cache[$item['value']];
continue;
}
// Force the array key to prevent duplicates.
$fc_ids[$item['value']] = $item['value'];
}
}
if ($fc_ids) {
$new_entities = array();
$terms = field_collection_item_load_multiple($fc_ids);
$new_items = $items;
// Iterate through the fieldable entities again to attach the loaded
// field collection data.
foreach ($entities as $id => $entity) {
$rekey = FALSE;
foreach ($items[$id] as $delta => $item) {
// Check whether the field collection field instance value could be
// loaded.
if (isset($terms[$item['value']])) {
// Replace the instance value with the term data.
$e = $terms[$item['value']];
$local_entity_cache[$item['value']] = $e;
$items[$id][$delta]['field_collection'] = $e;
foreach ($fields as $field_name => $field) {
if (isset($e->{$field_name})) {
$field_data = $e->{$field_name};
if (isset($field_data[$langcode])) {
$new_entities[$e->item_id] = $e->item_id;
$new_items[$e->item_id] = $field_data[$langcode];
}
}
}
}
elseif (!isset($items[$id][$delta]['field_collection'])) {
unset($new_items[$id][$delta]);
$rekey = TRUE;
}
}
if ($rekey && $new_items) {
// Rekey the items array.
$new_items[$id] = array_values($new_items[$id]);
}
}
field_collection_entity_preload($new_entities, $langcode, $new_items, $fields);
}
}