Skip to main content

Drupal 8: How to remove rows ($view->result) from a view programmaticaly

You can use hook_views_pre_render(ViewExecutable $view)  for altering the result data.

  function pixelthis_views_pre_render(ViewExecutable $view) {

    if ($view->id() === 'my_view' && $view->current_display === 'block_2') {

      $node = \Drupal::routeMatch()->getParameter('node');
      $result = $view->result;
      foreach ($view->result as $key => $value) {
        $parent_id = $value->_entity->get('field_event_parent_ref')->target_id;
        $event_node = \Drupal::entityTypeManager()->getStorage('node')->load($parent_id);
        if ($event_node->get('field_event_space_ref')->target_id !== $node->id()) {
           unset($view->result[$key]);
        }
      }
    }

 

views