20

Following what reported in the documentation for hook_views_query_alter(), I created a file named alterform.views.inc and put it into an enabled module called alterform. I added the following code in alterform.views.inc.but it doesn't work.

function alterform_views_query_alter(&$view, &$query) {
  switch($view->name) {
    case 'myViewName':  
      echo 'test test'; // test       
      $query->orderby[0] = "FIELD(node.type, 'story', 'page', 'productTypeC', 'productTypeD') ASC";
      $query->orderby[1] = "node_title ASC";
      break;
  }
}

why doesn't it output "test test"? What's wrong with my code?

there is still no output of test and views. what's wrong with my code? thank you

3
  • Never used it, but is it possible that you must include/require the alterform.views.inc inside your module and you didn't do it?
    – nonsenz
    Commented May 19, 2011 at 8:17
  • i have put alterform.views.inc in my alterform module file
    – enjoylife
    Commented May 19, 2011 at 8:20
  • how to t include/require the alterform.views.inc inside your module? thank you
    – enjoylife
    Commented May 19, 2011 at 8:21

5 Answers 5

22

Right. In order to "do" anything with views in a module, you have to implement hook_views_api. Without this, your module won't be recognised by Views, at all, ever (AFAIK).

Please add the following to your module code:

/**
 * Implementation of hook_views_api().
 */
function alterform_views_api() { // your module name into hook_views_api
  return array(
    'api' => 2,
    // might not need the line below, but in any case, the last arg is the name of your module
    'path' => drupal_get_path('module', 'alterform'),
  );
}

This should fix it so your module is at least recognised, then you can start debugging it :)

A good way to start debugging a view (i.e. a baby step) is to put dpm($view); right at the start of the views function you're implementing. In most cases, that will dump info onto the screen that will help you see where you're at. Would suggest doing this with devel.module installed/switched on.

Edit: This assumes Views 2 and Drupal 6. You'd best ask around if those aren't your weapons of choice.

1
  • This works fine :)
    – Kalidasan
    Commented Mar 9, 2015 at 10:14
4

you can test 3 things:

  • clear your cache
  • download and enable Devel,
  • use dpm() instead of echo()
  • test $view->name with dpm()
4
  • i have cleared the cache, and echo $view->name. but there is still no any output about the views name in the views block.
    – enjoylife
    Commented May 19, 2011 at 8:07
  • weird. I usualy dont rely on echo... By the way I noticed an little error in your code, there is a "}" that should'nt be... a paste error?
    – Manu
    Commented May 19, 2011 at 8:17
  • but when i print_r($views)there is still no any output about tit in the views block.
    – enjoylife
    Commented May 19, 2011 at 8:19
  • print_r($views) should not print anything but print_r($view) yes. Did you put this before the switch?
    – Manu
    Commented May 19, 2011 at 8:51
3

You need to put the yourmodule.views.inc file in the module directory, not within a subdirectory, or Views cannot automagically pick it up.

2
    $query->orderby[0] = "FIELD(node.type, 'story', 'page', 'productTypeC', 'productTypeD') ASC";

should be

    $query->orderby[0]['field'] = "field_name ASC";
1

if you are using a directory for yourmodule.views.inc, you need to mention its path in the module file, using code similar to the following one:

    'path' => drupal_get_path('module', 'mymodule') . '/subdirectory/';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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