I am trying to not show a certain view result using hook_views_pre_render but I didn't find the correct way to not display the result.

I did manage to have access to the view and change each result title to a test title, code:

function MODULENAME_views_pre_render(\Drupal\views\ViewExecutable $view) {

  if($view->id() == 'tasks') {
    foreach($view->result as $task) {
      $task->_entity->set('title', 'test - ' . time());

I want to use something like $task->show = false;, is there any way?

Thank you!

  • Wouldn't it be better to filter out those results in the first place? That way you won't get odd looking results if you use a pager for example
    – Clive
    Jun 21, 2017 at 18:39
  • How can I filter the results programmatically ? I tried to filter the results through ui in view but couldn't get the results I want. I want to filter a content type results by author (to the logged user) and by field_user (that is a referenced user in content type). When I filter by author all works and even when I filter by field_user but when I apply a OR condition to show both.. only results that reference the user appear.. And I can't understand why that happens. drupal.stackexchange.com/questions/238820/… I did ask, No answer Jun 22, 2017 at 9:13

1 Answer 1


Solved my problem with the following:

function MYMODULE_views_pre_render(\Drupal\views\ViewExecutable $view) {
  if($view->id() == 'tasks') {
    $i = 0;
    foreach($view->result as $value) {
      if($i == 2)

The code above does not show the 3rd result from the view results. Add logic as you need.

  • 1
    Use the right hook does the trick for me :) (pre_render and not preprocess_views_view).
    – Pauleau
    Aug 21, 2019 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.