欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

views_query_alter Doesn't Change my Query and Table in views

views_query_alter Doesn't Change my Query and Table in views

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(link is external) 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
I want to render a table with 2 columns ( users' id and users' email) in views(link is external) 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:

?(link is external)

1

2

3

4

5

6

7

8

9

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(link is external)

 

Thankyou for reading and i appreciate your help. :)

问题细节请点击上面我在stackoverflow 上放的问题。谢谢各位愿意帮我的朋友。

来自  http://drupalchina.cn/node/6286

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:

  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. 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 using dpm($query-> execute());, it will just return a query string which you could see in the first screen shot under title setting.

  5. 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




普通分类: