You are here

function multiversion_views_post_execute in Multiversion 8.2

Same name and namespace in other branches
  1. 8 multiversion.module \multiversion_views_post_execute()

Implements hook_views_post_execute().

File

./multiversion.module, line 245

Code

function multiversion_views_post_execute(ViewExecutable $view) {

  // Add deleted entities if we have rows for them.
  // When we want to get deleted entities using the _deleted field, entities
  // should be loaded with
  // \Drupal::entityManager()->getTypeStorage($entity_type)->loadDeleted($id) or
  // \Drupal::entityManager()->getTypeStorage($entity_type)->loadMultipleDeleted($ids),
  // otherwise the _entity field in the view result rows will be null.
  $base_field = $view->storage
    ->get('base_field');
  $table_info = $view->query
    ->getEntityTableInfo();
  $content_type_info = array_column($table_info, 'entity_type');
  if (is_array($view->result) && ($content_type = reset($content_type_info))) {
    $manager = \Drupal::service('multiversion.manager');
    $storage = \Drupal::entityTypeManager()
      ->getStorage($content_type);
    if ($manager
      ->allowToAlter($storage
      ->getEntityType())) {
      $ids = [];
      foreach ($view->result as $index => $row) {
        if (empty($row->_entity) && !empty($row->{$base_field})) {
          $ids[$index] = $row->{$base_field};
        }
      }
      $entities = $storage
        ->loadMultipleDeleted($ids);
      foreach ($view->result as $index => $row) {
        if (empty($row->_entity) && !empty($row->{$base_field}) && isset($entities[$row->{$base_field}])) {
          $view->result[$index]->_entity = $entities[$row->{$base_field}];
        }
        elseif (empty($row->_entity)) {
          unset($view->result[$index]);
        }
      }
    }
  }
}