将 drupal 视图设置为随机页码

发布于 2024-09-24 19:13:36 字数 394 浏览 3 评论 0原文

我安装了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还不是爱你 2024-10-01 19:13:36

我不知道如何从视图 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 :/

枫林﹌晚霞¤ 2024-10-01 19:13:36

另一种选择是随机化视图中的条目。因此,您的页面将始终是第 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 )

偷得浮生 2024-10-01 19:13:36

我刚刚创建了一个自动转到最后一页的自定义分页器,我认为它与您想要执行的操作有关:

在project.info中:

files[] = plugins/views_plugin_pager_last.inc

在project.module中:

function cvoxm_views_plugins(){
  return array(
    'pager' => array(
      'last' => array(
        'title' => t('Paged output, full pager and last by default'),
        'short title' => t('Full & Last'),
        'help' => t('Paged output, full Drupal style and last by default'),
        'handler' => 'views_plugin_pager_last',
        'help topic' => 'pager-last',
        'uses options' => TRUE,
      ),
    )
  );
}

plugins/views_plugin_pager_last.inc的内容是:

class views_plugin_pager_last extends views_plugin_pager_full {

  function pre_execute(&$query) {
    if(!isset($_GET['page'])){ // TODO: Should use pager_id
      // Go to last page
      $this->set_current_page($this->get_total_items() / $this->get_items_per_page() - 1 );
      $this->query(); // Rebuild query
      $this->update_page_info(); // Update info
    }
  }
}

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 :

files[] = plugins/views_plugin_pager_last.inc

In project.module :

function cvoxm_views_plugins(){
  return array(
    'pager' => array(
      'last' => array(
        'title' => t('Paged output, full pager and last by default'),
        'short title' => t('Full & Last'),
        'help' => t('Paged output, full Drupal style and last by default'),
        'handler' => 'views_plugin_pager_last',
        'help topic' => 'pager-last',
        'uses options' => TRUE,
      ),
    )
  );
}

And the content of plugins/views_plugin_pager_last.inc is :

class views_plugin_pager_last extends views_plugin_pager_full {

  function pre_execute(&$query) {
    if(!isset($_GET['page'])){ // TODO: Should use pager_id
      // Go to last page
      $this->set_current_page($this->get_total_items() / $this->get_items_per_page() - 1 );
      $this->query(); // Rebuild query
      $this->update_page_info(); // Update info
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文