将 drupal 视图设置为随机页码
我安装了views 2,并且创建了一个显示在首页的视图。
该视图显示一些页面链接(1 | 2 | 3 | 4 | ...等)。我想知道是否可以使视图从随机页面开始,而不是始终从第 1 页开始。
注意:我不想随机化显示,我只想随机化它加载的页面。
谢谢
可能的解决方案:
在views_pre_execute钩子中,我使用了这个:
$view->query->pager->set_current_page([random value]);
我不确定此时是否可以确定寻呼机中的总页数,但我将继续调查(给出的$view对象钩子中有大量带有数组和其他对象的属性,这使得这变得复杂)
I have views 2 installed and I have created a view that is displayed in the front page.
The view displays some page links ( 1 | 2 | 3 | 4 | ... etc). I want to know if it's possible to make the view start at a random page instead of always starting at page 1.
Note: I don't want to randomize the display I really just want to randomize the page it loads.
Thanks
Possible Solution:
In the views_pre_execute hook I used this:
$view->query->pager->set_current_page([random value]);
I am not sure I can determine the number of total pages in the pager at this time but I am going to keep investigating (The $view object given in the hook has tons of properties with arrays and other objects which makes this complicated)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道如何从视图 UI 执行此操作,但您应该能够使用 视图模块挂钩,在本例中可能是
hook_views_pre_execute
。不幸的是,这些文档实际上不存在,因此您需要在自定义模块中实现挂钩,并通过调试器(或 print、var_dump 等语句)检查传入的视图对象。您应该查找
$view->pager['current_page']
,您可以将其设置为随机页面。不幸的是,如果我正确地阅读了代码,此时尚未运行确定可能页数的计数查询,因此您要么必须使用“最佳猜测”,要么想出一种不同的方法来确定可供选择的适当范围...注意:这绝不意味着“权威”答案 - 只是我开始寻找的指针,因为到目前为止没有其他人回答过这个问题。我很可能会错过一个更明显/简单的解决方案:/
I do not know how to do this from the Views UI, but you should be able to achieve this using one of the views module hooks, in this case probably
hook_views_pre_execute
. Unfortunately, the documentation for these is practically non existing, so you'd need to implement the hook in a custom module and inspect the passed in view object via the debugger (or print, var_dump, etc. statements).You should look for
$view->pager['current_page']
, which you can set to a random page. Unfortunately, if I read the code correctly, the count query that determines the possible number of pages is not yet run at this point, so you'll either have to use a 'best guess', or come up with a different way to determine the proper range to select from...NOTE: This is in no way meant as an 'authoritative' answer - just a pointer where I'd start looking, since nobody else has answered this so far. I might well be missing a more obvious/easy solution :/
另一种选择是随机化视图中的条目。因此,您的页面将始终是第 1 页,但它实现了您每次访问网站时都能看到不同内容的目标。
在您的排序标准(在全局组中)中添加
全局:随机——随机显示顺序。
(受到 http://mydrupal.com/random_node_or_front_page_in_drupal_like_stumbleupon 的建议的启发)
Another option would be to randomize the entries in your views. So your page would always be page 1 but it achieves your objective of seeing something different every time come on your site.
In your sort criteria (in the Global Group) add
Global: Random -- Randomize the display order.
(Inspired by suggestion at http://mydrupal.com/random_node_or_front_page_in_drupal_like_stumbleupon )
我刚刚创建了一个自动转到最后一页的自定义分页器,我认为它与您想要执行的操作有关:
在project.info中:
在project.module中:
plugins/views_plugin_pager_last.inc的内容是:
I have just created a custom pager that goes automatically to last page and I think it is related to what your are trying to do :
In project.info :
In project.module :
And the content of plugins/views_plugin_pager_last.inc is :