How to get access of every result in a view with pagination with a hook in Drupal 9?

439 Views Asked by At

In drupal 9, I've been trying to get a specific value of every node result from a view and push them into an array. While debugging I was able to access the nodes of the view's current page and not from the other pages. Is there a way to achieve that with a hook_preprocess or any other hook?

Things I've tried:

  1. In a hook_preprocess_views_view I could access the node values from the page I was and not the rest, same with a hook_views_pre_render.

  2. Configured the view to Items per page option to 0 to bring me all the results in a single page and with a hook_views_pre_view to setItemsPerPage(10) but I couldn't get access to any result since this hook runs before the view fetches any.

1

There are 1 best solutions below

1
Vasileios Alyfantis On BEST ANSWER

To get the results of a view you could use the following function in your custom module file.

use Drupal\views\Views;

function get_view_results($view_id, $display_id) {
  $view = Views::getView($view_id);
  if (!isset($view)) {
    return [];
  }
  $view->setDisplay($display_id);
  $view->setItemsPerPage(0);

  $view->execute();
  $results = $view->result;
  return $results;
}