You are here

function relation_entity_collector_views_post_execute in Relation 8

Same name and namespace in other branches
  1. 8.2 relation_entity_collector/relation_entity_collector.module \relation_entity_collector_views_post_execute()
  2. 7 relation_entity_collector/relation_entity_collector.module \relation_entity_collector_views_post_execute()

Implements hook_views_post_execute().

Make sure entities are loaded even if only fields are used.

File

relation_entity_collector/relation_entity_collector.module, line 93
Relation Entity Collector Block.

Code

function relation_entity_collector_views_post_execute($view) {
  if (_relation_entity_collector_user_has_access()) {
    $properties = get_object_vars($view->query);
    if (!empty($properties['fields']) && !empty($view->result)) {
      foreach (entity_get_info() as $entity_type => $entity_info) {
        $map[$entity_info['base table']] = array(
          'id' => $entity_info['entity keys']['id'],
          'entity_type' => $entity_type,
        );
      }
      $collect = array();
      foreach ($view->query->fields as $alias => $field) {
        if (isset($field['table'])) {
          $table_name = $view->query->table_queue[$field['table']]['table'];
          if (isset($map[$table_name]) && $map[$table_name]['id'] == $field['field']) {
            $collect[$map[$table_name]['entity_type']] = $alias;
          }
        }
      }
      $ids = array();
      foreach ($view->result as $row) {
        foreach ($collect as $entity_type => $alias) {

          // Skip empty values, which may happen for entities that are obtained
          // via a non-required relationship in the view.
          if (!empty($row->{$alias})) {
            $ids[$entity_type][] = $row->{$alias};
          }
        }
      }
      foreach ($ids as $entity_type => $entity_ids) {
        $storage_handler = \Drupal::entityTypeManager()
          ->getStorage($entity_type);
        $storage_handler
          ->loadMultiple($entity_ids);
      }
    }
  }
}