views_query_alter Doesn't Change my Query and Table in views
My purpose
I want to render a table with 2 columns ( users' id and users' email) in views by writing a custom module. In order to alter the views, i want to change the query so that the table will be changed. Here the function that i choose is views_query_alter().
PROBLEM: The views reads my function but only return message from drupal_set_message command. And do not make any changes to Initial query and table.
Here is my function:
function my_module_views_query_alter(&$view, &$query) { if ($view->name == 'custom_views') { drupal_set_message("Hello"); $query =db_select('users','u'); $query -> addField('u','uid','Customer id'); $query -> addField('u','mail','Customer Email'); $results = $query -> execute(); } }
My purpose PROBLEM: The views reads my function but only return message from drupal_set_message command. And do not make any changes to Initial query and table. Here is my function:
Here are things that i've confirmed: 1. Flush all caches. More details about my question is on http://stackoverflow.com/questions/39379002/views-query-alter-doesnt-change-my-query-and-table-in-views
Thankyou for reading and i appreciate your help. :) 问题细节请点击上面我在stackoverflow 上放的问题。谢谢各位愿意帮我的朋友。 |
My purpose
I want to render a table with 2 columns ( users' id and users' email) in views by writing a custom module. In order to alter the views, i want to change the query so that the table will be changed. Here the function that i choose is views_query_alter().
PROBLEM: The views reads my function but only return message from drupal_set_message command. And do not make any changes to Initial query and table.
My Setting
I've made three files, which the names are my_module.info, my_module.module,my_module.views.inc respectively. Here is my setting:
In my views setting:
My views name & machine name are both custom_views. Base table is users. see pic
In my my_module.info:
name = My Module
description = Behold my awesome module.
core = 7.x
package = Other
In my_module.module:
<?php
/**
* Implements hook_views_api().
*/
function my_module_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'my_module'),
'template path' => drupal_get_path('module', 'my_module'),
);
}
In my_module.views.inc:
<?php
function my_module_views_query_alter(&$view, &$query) {
if ($view->name == 'custom_views') {
drupal_set_message("Hello");
$query =db_select('users','u');
$query -> addField('u','uid','Customer id');
$query -> addField('u','mail','Customer Email');
$results = $query -> execute();
}
}
Here are things that i've tested and confirmed:
- Flush all caches.
- my_module is enabled.
- views has successfully read my code in my_module.views.inc because i can see the drupal_set_message command gave me a message "Hello" in the views.
views_query_alter doesn't change the query in views but i can use dpm() to show my result.This what my dpm() message looks like when i used
dpm($query-> execute()->fetchAll());
, see pic. Without using fetchAll(), by just usingdpm($query-> execute());
, it will just return a query string which you could see in the first screen shot under title setting.File locations are correct.
Thankyou for reading and i appreciate your help. :)
来自 http://stackoverflow.com/questions/39379002/views-query-alter-doesnt-change-my-query-and-table-in-views