protected function TripalEntityController::attachLoad
3.x TripalEntityController.inc | protected TripalEntityController::attachLoad(&$queried_entities, $revision_id = FALSE,
$field_ids = array()) |
Override the attachLoad function.
A TripalEntity may have a large number of fields attached which may slow down the loading of pages and web services. Therefore, we only want to attach fields that are needed.
Parameters
$queried_entities: The list of queried
$revision_id: ID of the revision that was loaded, or FALSE if the most current revision was loaded.
$field_ids:
1 call to TripalEntityController::attachLoad()
- TripalEntityController::load in tripal/
includes/ TripalEntityController.inc - Override the load function.
File
- tripal/
includes/ TripalEntityController.inc, line 573
Class
- TripalEntityController
- TripalEntityController extends DrupalDefaultEntityController.
Code
protected function attachLoad(&$queried_entities, $revision_id = FALSE,
$field_ids = array()) {
// Attach fields.
if ($this->entityInfo['fieldable']) {
if ($revision_id) {
$function = 'field_attach_load_revision';
}
else {
$function = 'field_attach_load';
}
foreach ($queried_entities as $id => $entity) {
$info = entity_get_info($entity->type);
$field_cache = array_key_exists('field cache', $info) ? $info['field cache'] : FALSE;
$bundle_name = $entity->bundle;
// Iterate through the field instances and find those that are set to
// 'auto_attach' and which are attached to this bundle. Add all
// fields that don't need auto attach to the field_ids array.
$instances = field_info_instances('TripalEntity', $bundle_name);
foreach ($instances as $instance) {
$field_name = $instance['field_name'];
$field = field_info_field($field_name);
$field_id = $field['id'];
// Add this field to the entity with default value.
if (!isset($queried_entities[$id]->$field_name)) {
$queried_entities[$id]->$field_name = array();
}
// Options used for the field_attach_load function.
$options = array();
$options['field_id'] = $field['id'];
// The cache ID for the entity. We must manually set the cache
// because the field_attach_load won't do it for us.
$cfid = "field:TripalEntity:$id:$field_name";
// Check if the field is cached. if so, then don't reload.
if ($field_cache) {
$cache_data = cache_get($cfid, 'cache_field');
if (!empty($cache_data)) {
$queried_entities[$id]->$field_name = $cache_data->data;
$queried_entities[$id]->{$field_name}['#processed'] = TRUE;
continue;
}
}
// If a list of field_ids is provided then we specifically want
// to only load the fields specified.
if (count($field_ids) > 0) {
if (in_array($field_id, $field_ids)) {
$function($this->entityType, array($entity->id => $queried_entities[$id]),
FIELD_LOAD_CURRENT, $options);
// Cache the field.
if ($field_cache) {
cache_set($cfid, $entity->$field_name, 'cache_field');
}
$queried_entities[$id]->{$field_name}['#processed'] = TRUE;
}
}
// If we don't have a list of fields then load them all, but only
// if the instance is a TripalField and it is set to not auto
// attach then we will ignore it. It can only be set by providing
// the id in the $field_id array handled previously.
else {
// We only load via AJAX if empty fields are not hidden.
$bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
$hide_variable = tripal_get_bundle_variable('hide_empty_field', $bundle->id, 'hide');
if (array_key_exists('settings', $instance) and
array_key_exists('auto_attach', $instance['settings']) and
$instance['settings']['auto_attach'] == FALSE and
$hide_variable == 'show') {
// Add an empty value. This will allow the tripal_entity_view()
// hook to add the necessary prefixes to the field for ajax
// loading.
$queried_entities[$id]->$field_name['und'][0]['value'] = '';
$queried_entities[$id]->{$field_name}['#processed'] = FALSE;
}
else {
$function($this->entityType, array($entity->id => $queried_entities[$id]),
FIELD_LOAD_CURRENT, $options);
// Cache the field.
if ($field_cache) {
cache_set($cfid, $entity->$field_name, 'cache_field');
}
$queried_entities[$id]->{$field_name}['#processed'] = TRUE;
}
}
}
}
}
// Call hook_entity_load().
foreach (module_implements('entity_load') as $module) {
$function = $module . '_entity_load';
$function($queried_entities, $this->entityType);
}
// Call hook_TYPE_load(). The first argument for hook_TYPE_load() are
// always the queried entities, followed by additional arguments set in
// $this->hookLoadArguments.
$args = array_merge(array($queried_entities), $this->hookLoadArguments);
foreach (module_implements($this->entityInfo['load hook']) as $module) {
call_user_func_array($module . '_' . $this->entityInfo['load hook'], $args);
}
}