Ace*_*Ace 11 php sorting drupal drupal-views
我正在尝试获取视图的结果 - 使用函数views_get_view_result()- 并以一种我在Views界面中无法做到的方式对数组进行排序.到现在为止还挺好.我有一个$ rows变量,包含我需要的所有东西.
现在......我怎么把它放回去?:)在我需要这种之前,我曾经使用过views_embed_view(),但我不能再那样做了.
感谢你对此有任何帮助,感觉我已经接近破解了!
$important_var = important_function();
$result = views_get_view_result($view, $display, $args);
$result = sorting_function($result, $important_var);
//TODO: Put the result back into the view
Run Code Online (Sandbox Code Playgroud)
Hen*_*pel 18
视图模块为"外部"操作提供了一些钩子,就像Drupal核心一样.
您可以hook_views_pre_render(&$view)在自定义模块中实现并操作以下可用的结果数组$view->result:
/**
* Implementation of hook_views_pre_render()
*
* @param view $view
*/
function YourModuleName_views_pre_render(&$view) {
// Check if this is the view and display you want to manipulate
// NOTE: Adjust/Remove the display check, if you want to manipulate some/all displays of the view
if ('YourViewName' == $view->name && 'YourDisplayName' == $view->current_display) {
// EXAMPLE: Just reverse result order
// TODO: Replace with your desired (re)ordering logic
$view->result = array_reverse($view->result);
}
}
Run Code Online (Sandbox Code Playgroud)
在视图生成过程的中间调用钩子,在所有结果数据已经组装之后,但在实际输出被渲染之前,结果数组的更改将反映在视图最终输出中.
编辑:或者,您可以通过复制views_get_view_result()函数的行为来"手动"处理视图,但不是返回结果,而是操纵它并继续渲染视图:
function yourModule_get_custom_sorted_view($display_id = NULL) {
// As the custom sorting probably only works for a specific view,
// we 'demote' the former $name function parameter of 'views_get_view_result()'
// and set it within the function:
$name = 'yourViewName';
// Prepare a default output in case the view definition can not be found
// TODO: Decide what to return in that case (using empty string for now)
$output = '';
// Then we create the result just as 'views_get_view_result()' would do it:
$args = func_get_args();
if (count($args)) {
array_shift($args); // remove $display_id
}
$view = views_get_view($name);
if (is_object($view)) {
if (is_array($args)) {
$view->set_arguments($args);
}
if (is_string($display_id)) {
$view->set_display($display_id);
}
else {
$view->init_display();
}
$view->pre_execute();
$view->execute();
// 'views_get_view_result()' would just return $view->result here,
// but we need to go on, reordering the result:
$important_var = important_function();
$view->result = sorting_function($result, $important_var);
// Now we continue the view processing and generate the rendered output
// NOTE: $view->render will call $view->execute again,
// but the execute method will detect that it ran already and not redo it.
$output = $view->render();
// Clean up after processing
$view->post_execute();
}
return $output;
}
Run Code Online (Sandbox Code Playgroud)
注意:这是很多代码重复,因此容易出错 - 我不建议这样做,宁愿选择上面的钩子实现,试图找到一种方法来从中获取'$ important_var'.
spe*_*nty 10
根据您的排序逻辑,您可能尝试使用hook_views_query_alter()直接在查询中实现排序.
这可能有点棘手,您可以熟悉views_query对象.
这是一个真实世界的例子,我根据页面的上下文应用了一个连接规则,然后另外添加了排序规则.
/**
* Implementation of hook_views_query_alter().
*/
function yourmodule_views_query_alter(&$view, &$query) {
if ($view->name == 'view_projects' && $view->current_display == 'panel_pane_4') {
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
if ($client_nid = $node->field_ref_client[0]['nid']) {
$query->table_queue['node_node_data_field_ref_client']['join']->extra = "field_ref_client_nid = " . $client_nid;
$query->add_orderby('node', NULL, 'DESC', 'node_node_data_field_ref_client_nid');
$query->add_orderby('node', 'created', 'DESC');
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅:http://drupalcontrib.org/api/function/hook_views_query_alter/6