function views_handler_field_field::post_execute

Load the entities for all fields that are about to be displayed.

Overrides views_handler::post_execute

File

modules/field/views_handler_field_field.inc, line 655

Class

views_handler_field_field
A field that displays fieldapi fields.

Code

public function post_execute(&$values) {
    if (!empty($values)) {
        // Divide the entity ids by entity type, so they can be loaded in bulk.
        $entities_by_type = array();
        foreach ($values as $key => $object) {
            if (isset($this->aliases['entity_type']) && isset($object->{$this->aliases['entity_type']}) && isset($object->{$this->field_alias}) && !isset($values[$key]->_field_data[$this->field_alias])) {
                $entity_type = $object->{$this->aliases['entity_type']};
                if (empty($this->definition['is revision'])) {
                    $entity_id = $object->{$this->field_alias};
                    $entities_by_type[$entity_type][$key] = $entity_id;
                }
                else {
                    $revision_id = $object->{$this->field_alias};
                    $entity_id = $object->{$this->aliases['entity_id']};
                    $entities_by_type[$entity_type][$key] = array(
                        $entity_id,
                        $revision_id,
                    );
                }
            }
        }
        // Load the entities.
        foreach ($entities_by_type as $entity_type => $entity_ids) {
            $entity_info = entity_get_info($entity_type);
            if (empty($this->definition['is revision'])) {
                $entities = entity_load($entity_type, $entity_ids);
                $keys = $entity_ids;
            }
            else {
                // Revisions can't be loaded multiple, so we have to load them
                // one by one.
                $entities = array();
                $keys = array();
                foreach ($entity_ids as $key => $combined) {
                    list($entity_id, $revision_id) = $combined;
                    $entity = entity_load($entity_type, array(
                        $entity_id,
                    ), array(
                        $entity_info['entity keys']['revision'] => $revision_id,
                    ));
                    if ($entity) {
                        $entities[$revision_id] = array_shift($entity);
                        $keys[$key] = $revision_id;
                    }
                }
            }
            foreach ($keys as $key => $entity_id) {
                // If this is a revision, load the revision instead.
                if (isset($entities[$entity_id])) {
                    $values[$key]->_field_data[$this->field_alias] = array(
                        'entity_type' => $entity_type,
                        'entity' => $entities[$entity_id],
                    );
                }
            }
        }
        // Now, transfer the data back into the resultset so it can be easily
        // used.
        foreach ($values as $row_id => &$value) {
            $value->{'field_' . $this->options['id']} = $this->set_items($value, $row_id);
        }
        unset($value);
    }
}