【问题标题】:How to sort View results programmatically?如何以编程方式对查看结果进行排序?
【发布时间】:2010-03-18 16:04:57
【问题描述】:

我正在尝试获取视图的结果 - 使用函数 views_get_view_result() - 并以我无法在视图界面中执行的方式对数组进行排序。到现在为止还挺好。我有一个包含所有我需要的东西的 $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

【问题讨论】:

  • 您可以编辑 Views 界面生成的 SQL 查询以在查询中进行排序吗?
  • 不,排序取决于在该上下文中不可用的一些变量。

标签: php drupal sorting drupal-views


【解决方案1】:

视图模块 provides some hooks 用于“外部”操作,就像 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);
  }
}

该钩子在视图生成过程的中间被调用,在所有结果数据已经组装之后,但在实际输出被渲染之前,因此对结果数组的更改将反映在视图最终输出中。

编辑: 或者,您可以“手动”处理视图,方法是复制 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;
}

注意:这是很多代码重复,因此容易出错 - 我不推荐这样做,宁愿使用上面的钩子实现,试图找到一种方法来访问你的 ' $important_var' 来自其中。

【讨论】:

  • 听起来不错!但是,如果我有一些排序所需的外部变量呢?
  • @Ace:您可以将外部变量内容获取到钩子实现中(通过从那里调用important_function(),或通过先前填充的全局,或通过调用'get_x()'函数使用静态存储先前计算的变量),或者您需要“手动”进行视图处理 - 我将更新我的答案以显示后一个选项。
  • 使用 hook_views_pre_render 不适用于寻呼机。它只会对当前页面的结果进行排序。
【解决方案2】:

根据您的排序逻辑,您可能会尝试使用 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'); 
      } 
    }
  } 
}

见:http://drupalcontrib.org/api/function/hook_views_query_alter/6

【讨论】:

  • 另一个带有 SQL ORDER BY CASE 语句的示例在 this answer 中。允许列表字段的任意排序顺序。
  • 感谢在 add_orderby 中使用表别名的示例 :)
【解决方案3】:

我使用这样的代码:

/**
 * Implementation of hook_views_post_execute()
 *
 * @param view $view
 */
function YourModuleName_views_post_execute(&$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);
  }
}

hook_views_pre_render 不适合我。

【讨论】:

    【解决方案4】:

    我把上面的例子结合起来,改写如下。就我而言,我必须单独对项目进行排序。

    注意:我没有包含连接条件,也没有尝试使用自定义字段。但我觉得你可以很容易地弄清楚。

    /** 
     * Implementation of hook_views_query_alter(). 
     */ 
    function yourmodule_views_query_alter(&$view, &$query) { 
      if ($view->name == 'your_view_name' && $view->current_display == 'your_pane_name') {
        if ($query->orderby[0]['field'] == 'my_order_field') {
          $query->orderby[0]['field'] = "FIELD(my_order_field, 'ord3','ord1','ord2')";
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-25
      • 1970-01-01
      • 2021-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多