英文:
How to get access of every result in a view with pagination with a hook in Drupal 9?
问题
在Drupal 9中,我一直在尝试获取视图中每个节点结果的特定值并将它们推送到一个数组中。在调试过程中,我能够访问视图当前页面的节点,而不能访问其他页面的节点。
是否有一种方法可以通过hook_preprocess或任何其他hook来实现这一点?
我尝试过的方法:
-
在
hook_preprocess_views_view
中,我可以访问当前页面的节点值,但不能访问其他页面的节点值,hook_views_pre_render
也是一样的。 -
配置视图的"每页项目数"选项为0,以在单个页面中获取所有结果,并使用
hook_views_pre_view
来setItemsPerPage(10)
,但由于此hook在视图获取任何结果之前运行,所以无法访问任何结果。
英文:
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:
-
In a
hook_preprocess_views_view
I could access the node values from the page I was and not the rest, same with ahook_views_pre_render
. -
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
得分: 0
要获取视图的结果,您可以在自定义模块文件中使用以下函数。
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;
}
英文:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论