Mysql 如何使用模块修改Drupal7.x中视图的输出

Mysql 如何使用模块修改Drupal7.x中视图的输出,mysql,drupal,module,drupal-7,drupal-views,Mysql,Drupal,Module,Drupal 7,Drupal Views,我想修改正在运行的查询,以便在视图中创建一个表,但我很难做到这一点,因此为了保持简单,我使用了一个简单的视图,它只列出表中的用户,并试图修改查询,使表具有两列。我一直在看Drupal查询的文档,试图找到教程,并查看关于其他人如何在Drupal和Views中修改查询的博客文章,但到目前为止,还没有人工作过,也没有人展示过如何做像这样简单的事情,或者我尝试按照他们所做的事情创建简单的查询语句,但没有结果 我主要尝试使用hook\u views\u pre\u execute(),但也尝试过。这就是我

我想修改正在运行的查询,以便在视图中创建一个表,但我很难做到这一点,因此为了保持简单,我使用了一个简单的视图,它只列出表中的用户,并试图修改查询,使表具有两列。我一直在看Drupal查询的文档,试图找到教程,并查看关于其他人如何在Drupal和Views中修改查询的博客文章,但到目前为止,还没有人工作过,也没有人展示过如何做像这样简单的事情,或者我尝试按照他们所做的事情创建简单的查询语句,但没有结果

我主要尝试使用
hook\u views\u pre\u execute()
,但也尝试过。这就是我的代码目前的样子:

<?php
/**
 * Implements hook_views_query_alter().
 */
// This function is called right before the execute process.
function my_module_views_pre_execute(&$view) {
    if ($view->name == 'page' && $view->current_display == 'page') { //checks name of the view and what type it is
        drupal_set_message("I can make changes to the view here.."); //diagnostic information, shows we can make changes to that view
        $query =db_select('node','n')//select base table, alias is n.
            ->fields('n',array('nid', 'title', 'created', 'uid')); //adding fields nid, title, created, uid.
        dpm($query->execute()->fetchAll()); // diagnostic information, shows whats being returned by the query 
        $query->execute()->fetchAll();

        $view->build_info['query'] = $query;
    }
}

我只举了nid的例子,这里我可以为您提供一个更改视图输出的想法

/**
 * 
 * @param type $view
 * @param type $query
 * Implements hook_views_query_alter(). This function is Used when we need to Alter the query before executing the query.
 */
function mymodule_views_query_alter(&$view, &$query) {
    if ($view->name == 'VIEW_NAME' && $view->current_display == 'PAGE_NAME') {
    drupal_set_message("I can make changes to the view here.."); //diagnostic information, shows we can make changes to that view
        $query =db_select('node','n')//select base table, alias is n.
            ->fields('n',array('nid', 'title', 'created', 'uid')); //adding fields nid, title, created, uid.
        dpm($query->execute()->fetchAll()); // diagnostic information, shows whats being returned by the query 
        $result = $query->execute()->fetchAll();
    $nids = array();
    foreach ($result as $value) {
      $nids[] = $value->nid;
     }
    $view->query->where[1]['conditions'][] = array('field' => "node.nid", "value" => $nids, "operator" => "IN");
  }
}
hook\u views\u pre\u execute()在查询构建过程中太晚了,所以我认为应该使用hook\u views\u query\u alter()


谢谢

谢谢。我已将函数更改为views\u query\u alter,但新的问题是:视图已成功读取函数并返回drupal集合消息,但函数未对查询和表应用任何更改。与views\u pre\u execute函数相比,它成功地更改了查询,但没有对表应用某些更改。例如,我可以使用views\u pre\u execute函数来更改第一列中的内容。但是,即使在代码中添加了字段,也无法从函数中添加新列。