欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

drupal hook view pager

 

例如,您可以看到在查看特定节点类型时显示的视图,但您希望每个单独节点的寻呼机设置不同。我正在想一个CCK字段,如“寻呼机设置”,然后给它一个整数选项xy。但是我不知道是否可以以某种方式动态地将该字段插入到“视图”设置中。还有另外一种方法呢?

分享改善这个问题
 

正确答案 
您想要使用的视图钩子
hook_views_pre_build是在构建查询之前调用的。现在这是假设你有一些基本的模块开发经验,你熟悉的视图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-2.x编写的。

希望这可以帮助。

分享改善这个答案
 
   
真棒,谢谢 我还没有任何模块开发经验,但是我想要完成几件事情。我确实掌握了PHP,所以我可以按照上面粘贴的代码块中的内容进行跟踪。我觉得这很好帮助我很多。谢谢。 -  周杰伦 03月30日在'11 6:46
1 
对于Views 3.x,相关代码应该更改为$view->items_per_page = $pager_count; -  stevenw00 Jul 29 '14 at 8:21
   
是否可以动态设置每个页面的偏移量? -  shekoufeh 2月20日8时19分

要在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;
  }
}

这应该工作!;)

分享改善这个答案
 
   
优秀!我想限制给定页面上的项目数量取决于查询返回的项目。将尽快查看此方法。 -  詹斯 5月19日13日13时22分
   
它没有正常工作。放电的行不会在下一页上显示。所以你最终缺少内容。需要更多的调查。 -  詹斯 5月19日13时14分44分

对于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的代码示例。

分享改善这个答案
 

你应该使用视图预处理功能

/*
 * Implementation of hook_views_pre_render().
 */
function MYMODULE_views_pre_render(&$view){
  // $view->name
  // $view->current_display
  // ...
  // look for other variables in $view object
}
分享改善这个答案
 
   
“预处理”是用于主题,pre_render太晚了(他的查询已经运行) - pre_build钩子好多了。 -  mojzis 1月6日'13在11:32

@tanmayk的代码为我做了诀窍。在hook_views_pre_render中添加了这两行代码

$view->query->pager->total_items = $nr;
$view->query->pager->update_page_info();

但是我不需要将结果添加到视图中。

 
 
 



来自  http://drupal.stackexchange.com/questions/1561/is-it-possible-to-dynamically-set-views-pager-settings

 

 

Say for instance, you have a view that displays when viewing a certain node type, but you want the pager settings to be different for each individual node. I was thinking of making a CCK field like "pager setting" and then give it an integer option of x-y. But I don't know if it's possible to somehow dynamically plug that field into the Views' settings. Or is there another way to do this maybe?

shareimprove this question
 

正确答案 
The views hook that you would want to use is hook_views_pre_build which is called before the query is built. Now this is assuming you have some basic module development experience and that you are familiar with the views api.

You should be able to do :

/*
 * 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;
    }
  }
}

Above we're using a views hook that's called before the view query is built that way the pager and everything else will reflect the change.

Word of caution: views hooks should only be used if you understand whats going on. The above code is written for views-2.x.

Hope this helps.

shareimprove this answer
 
   
Awesome, thank you. I don't have any module development experience yet, but I'm starting out on it for a couple of things I want to accomplish. I do have a working grasp of PHP, so I can follow along what's happening in that code chunk you pasted above. I think this well help me a lot. Thank you. – Jay Mar 30 '11 at 6:46
1 
For Views 3.x the relevant code should be changed to $view->items_per_page = $pager_count; – stevenw00 Jul 29 '14 at 8:21
   
is it possible to set offset of each page dynamically too? – shekoufeh Feb 20 at 8:19

To update views result and pager in hook_views_pre_render, you can do following:

<?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;
  }
}

This should work!! ;)

shareimprove this answer
 
   
Excellent! I wanted to limit the number of items on a given page depended on the items returned by the query. Will check out this method ASAP. – Jens May 19 '13 at 13:22
   
It does not exactly work. The rows you discharge does not turn up on the next page. So you end up with missing content. Needs more investigation. – Jens May 19 '13 at 14:44

For Drupal 7, Only should write the following:

$view->items_per_page = $pager_count;

In the example:

/**
 * 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;
    }
  }
}

I use code example by @ericduran.

shareimprove this answer
 

You should to use views preprocess function

/*
 * Implementation of hook_views_pre_render().
 */
function MYMODULE_views_pre_render(&$view){
  // $view->name
  // $view->current_display
  // ...
  // look for other variables in $view object
}
shareimprove this answer
 
   
"preprocess" is for theming and pre_render is too late (he query was allready run)- the pre_build hook is much better. – mojzis Jan 6 '13 at 11:32

@tanmayk 's code did the trick for me. In hook_views_pre_render added just these 2 lines of code

$view->query->pager->total_items = $nr;
$view->query->pager->update_page_info();

But I didn't need to add the results to the view.


来自  http://drupal.stackexchange.com/questions/1561/is-it-possible-to-dynamically-set-views-pager-settings


普通分类: