Drupal 7 开发内部培训资料, 分头诗人

views_query_alter Doesn't Change Query and Table in views

赞成!
0
否决!

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();
    }
}

Here are things that i've confirmed:

1. Flush all caches.
2. my_module is enabled.
3. 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.
4. File locations are correct.

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 上放的问题。谢谢各位愿意帮我的朋友。

1 个回答

赞成!
0
否决!

"If you have the $query variable, then you can do something like drupal_set_message((string) $query) before you run $query->execute(). Otherwise you can download and enable the Devel module and call dpq($query)."

( source: https://api.drupal.org/api/drupal/includes!database!database.inc/functio... )