如果视图为空,则隐藏视图视图的链接

dus*_*san 2 drupal drupal-6

我有一个带有Views模块的Drupal 6.14站点.我有一个视图,在主链接上我放置了一个链接到视图.

只有在视图为空时,才能隐藏主菜单中的链接?

jhe*_*rom 6

您可以通过preprocess_page(THEMENAME_preprocess_page(&$ vars)或MODULENAME_preprocess_page(&$ vars))的主题或模块实现来执行此操作,但上面的mac是正确的,因为在运行它们之前不知道它们是否为空,所以会有性能受到打击.

在该函数中,您应该可以访问结构化主链接数组,因此您可以运行视图:

 $view = views_get_view('view_name');
 // Swap out 'default' for a different display as needed. Also, $args are arguments, and can be left out if not applicable.
 $output = $view->preview('default', $args);
 if (empty($view->result)) {
   // The view has no results, alter the primary links here to remove the link in question.
 }
Run Code Online (Sandbox Code Playgroud)

  • +1,但还有一句话:`$ view-> preview`不仅会构建视图,还会渲染它,这是不必要的开销.`$ view-> execute`也会填充结果属性,但会节省额外的处理时间(它只是构建并运行查询). (2认同)