Drupal 9 image field migration

As you know views has good pager configuration, where you can set how many items skip (offset), how many display etc.
But it has no info of minimum items to display!
For example we dont want to show the block if there are less than 5 items in results. Maybe it will break styles or somthing else.
In this case a simple condition in hook_views_post_execute will be useful :

/**
 * Implements hook_views_post_execute().
 */
function my_commerce_views_post_execute(ViewExecutable $view) {
  // Do not display block if there is less than 5 products.
  if ($view->id() == 'my_view_id'
    && $view->current_display == 'my_display_name'
    // && \Drupal::currentUser()->isAnonymous()
  ) {
    if (count($view->result) < 5) {
      $view->build_info['fail'] = TRUE;
    }
  }
} 

Also additional condition could be added to prevent showing block only for anonymous users (\Drupal::currentUser()->isAnonymous()).

Add new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed.
CAPTCHA
 ooooo          o8o   oooooo   oooooo     oooo  oooo         oooooooooooo   oooooooooooo 
`888' `"' `888. `888. .8' `888 `888' `8 d'""""""d888'
888 oooo `888. .8888. .8' 888 oooo 888 .888P
888 `888 `888 .8'`888. .8' 888 .8P' 888oooo8 d888'
888 888 `888.8' `888.8' 888888. 888 " .888P
888 o 888 `888' `888' 888 `88b. 888 d888' .P
o888ooooood8 o888o `8' `8' o888o o888o o888o .8888888888P


Enter the code depicted in ASCII art style.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.