如何通过代码实现Views结果调用?

注意事项:
如支付后未自动显示完整内容,可点击“已支付?点此查询订单”进行查看。
如遇内容不符或缺失,请联系内容作者或平台客服(工作日 9:00-18:00)。

  导读:Views提供了方便的方式让我们无需编写复杂的代码便可以构造数据库请求,并提供了多种实用的显示格式(如列表、表格等)。但有时,也会遇见通过代码来调用Views结果的情况。

  本文除了提供最终的实现代码外,作者解决问题的思路也可以略窥一二,文章虽短但也值得品鉴学习。

  除了在代码中调用Views结果之外,使用Views作为数据源也是高级Drupal工程师常常会用的办法,其优势有以下几点:

  1. 可以使用Views构造数据库请求,不用手动编写数据库查询语句(节省工作量,降低难度还可以减少错误)
  2. 可以方便地调整Views设置(例如改变过滤条件等),而不需要修改源代码
  3. 可以自行构造模板/输出的HTML结构,清理掉冗余的div嵌套

  如果希望使用第三方模块实现Views作为数据源的功能,可以参考 Views Datasource 模块。

========== 原文分隔线 ==========

There are scenarios when outside of a view we need to fetch results of any particular view. This is a very specific case when we just want the records compiled by Drupal Views. In that case obviously views api or views hooks are of no use, as we are not looking for event driven activities. It’s just the results needs to be fetched using views because of the complexity of the criterion on which these results are computed.

We won’t go for this approach when we want simple results like all the nodes of a specific content type sorted alphabetically. In that case, simply db_select would be better choice again depending on various project specific factors. In general, we can’t actually tag any approach as the best or optimal for general purpose as these are scenario specifics.

In our case, the scenario is we have a very complex view having good amount of filters or simply i would say the corresponding sql query is complex. Now in that case outside of the view we have two options to get results:

  1. Function views_get_view_result()
  2. Executing corresponding SQL Query

Approach 1: Function views_get_view_result():

// To get the result of a view.
views_get_view_result($name, $display_id = NULL)  // views.module

This looks promising, provided by views module. Accepts views name and display_id as parameter and simply fetches you the result. What we wanted is accomplished.

Limitation: In case of pagination, probably we want to get all the results and this approach fails there. We cannot fetch all the results if the corresponding view limits results to specific number per page. So, what should we do next!


Let’s look at our second approach, would that be useful in our case?


Approach 2: Executing corresponding SQL Query:

We can directly execute static sql query, which is a good solution for this scenario. But when we want to enjoy further flexibilities of this approach, say we want to change the query a bit then what? It is possible with this approach but the method is not recommendable. Infact best way to proceed further using this approach is to convert static query into dynamic drupal query which is a tedious process. So, how to achieve this?


Final Solution:

Looking at how the views work, we got a very promising structured way of solving all our related problems. Views provide several methods for views object which can be used to get all the results with/without customization of a particular view. Let’s see how:

$view = views_get_view(‘example_view’);
$view->build($display_id);
$view->query->limit = 0;
$view->execute();
$results = $view->result;

So, we finally have all the results of a view. As far as the above implementation is concerned, we basically are fetching a view, then building a specific display of that view and after customization we are finally executing it to get the results. In simple language, we are creating a similar temporary instance to get the desired results.


Hope this helps you (smile).  

看完了?还不过瘾?点此向作者提问