Answers:
您要使用的视图挂钩hook_views_pre_build
是在构建查询之前调用的。现在,这是假设您具有一些基本的模块开发经验,并且您熟悉Views api。
您应该能够:
/*
* Implementation of hook_views_pre_build().
*/
function hook_views_pre_build(&$view) {
// Make sure this is only for the specific view you want to modified
if ($view->name == "foo_bar") {
// Get the x-y value from where you're storing it (in your example the node object).
$pager_count = get_count_for_this_node();
// Lets also make sure that this is a number so we won't destroy our view.
if (is_numeric($pager_count)) {
// Now lets set the pager item to what ever out count is.
$view->pager['items_per_page'] = $pager_count;
}
}
}
在上面,我们使用了一个视图挂钩,该挂钩在构建视图查询之前被调用,这样分页器和其他所有内容都会反映出更改。
提醒您:仅当您了解发生了什么情况时,才应使用views hooks。上面的代码是为views-2.x编写的。
希望这可以帮助。
$view->items_per_page = $pager_count;
对于Drupal 7,仅应编写以下内容:
$view->items_per_page = $pager_count;
在示例中:
/**
* Implements hook_views_pre_build().
*/
function module_name_views_pre_build(&$view) {
if ($view->name == "foo_bar" && $view->current_display == 'foo_display') {
$pager_count = get_count_for_this_node();
if (is_numeric($pager_count)) {
$view->items_per_page = $pager_count;
}
}
}
我使用@ericduran的代码示例。
要在hook_views_pre_render中更新视图结果和寻呼机,您可以执行以下操作:
<?php
/**
* Implementation of hook_views_pre_render().
*/
function MODULENAME_views_pre_render(&$view) {
if ($view->name == 'my_view' && $view->current_display == 'my_display') {
// View result update logic.
// e.g.
// $result = array();
// foreach ($view->result as $k => $row) {
// if (whatever is your condition) {
// $result[$k] = $row;
// }
// }
// Assuming $result has data as per your logic.
// Update the pager according to result.
$view->query->pager->total_items = count($result);
$view->query->pager->update_page_info();
// Add results to view.
$view->result = $result;
}
}
这应该工作!!;)