1

I created a module to alter the query executed by a view, but i cannot figure it out how to test it ... I mean where should i see the results ? I've created the following files:

my_module.info
my_module.module
my_module.views.inc

in the second file i have:

function my_module_views_api() {
  return array(
    'api' => 3, 
    'path' => drupal_get_path('module', 'my_module'), 
    'template path' => drupal_get_path('module', 'my_module'),
  );
}

and in the third one:

function my_module_hook_views_query_alter(&$view, &$query)  {
  var_dump($view);
}

Where should i be able to see the result of var_dump ?

  • Have you created any Views ? Views alter will be invoked whenever a view is rendered in front end. – GoodSp33d Nov 28 '12 at 10:58
3

hook_views_query_alter() is supposed to alter the query passed as second parameter, or the view object passed as first argument, not to output anything. If you want to debug the hook, you should use code similar to the following one.

watchdog('my_module', 'View: @view', array('@view' => print_r($view, TRUE)), WATCHDOG_DEBUG);

The name you gave to your hook is wrong: You just need to replace hook with the short name of your module. For a hook like hook_views_query_alter(), the function name is not my_module_hook_views_query_alter(), but my_module_views_query_alter().

| improve this answer | |
  • 2
    Good spot... :) – Clive Nov 28 '12 at 10:47
2

Nowhere probably, you're dumping the var straight out to the buffer which isn't the 'done thing' in Drupal :) (unless you're taking care of the page rendering yourself for whatever reason, but that's a different story)

For debugging, get a copy of the Devel module and use the excellent dpm() function like so:

function my_module_hook_views_query_alter(&$view, &$query) {
  dpm($view);
}

That'll give you a nice output to the message area similar to:

enter image description here

You can navigate through and drill down into expandable variables...all in all it makes debugging simple arrays and objects very easy.

| improve this answer | |
  • i see .. i wrote var_dump just as an example ... I've changed to dsm , where should i go to see the result as the picture you provided? – Vlasin Nov 28 '12 at 10:47
0

Firstly, your hook name is wrong. It should be my_module_views_query_alter() instead of my_module_hook_views_query_alter() ( kiamlaluno has mentioned )
Assuming you have a view and its machine name is "my_view" and its menu path is "example/myview".
The var_dump result will be rendered in the page http://yousite.com/example/myview

In the hook, you may alter the query of the view by checking its view name, for example

function my_module_views_query_alter(&$view, &$query) {
  var_dump($view);  // this will be displayed in all views pages
  switch($view->name){
    case 'my_view' :    // this is machine name of the view
      // query alter here
      var_dump($view);  // this will be displayed only in this view page, for example, http://yousite.com/example/myview
    break;

    case 'another_view' :

    break;
  }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.