4

I need to modify the query of one of my views. I've found hook_views_query_alter(), but can't for the life of me figure out what I'm supposed to do with it. According to the API,

This hook should be placed in MODULENAME.views.inc and it will be auto-loaded. MODULENAME.views.inc must be in the directory specified by the 'path' key returned by MODULENAME_views_api(), or the same directory as the .module file, if 'path' is unspecified.

OK, so I know to put the function with the hook in a views.inc file. But what do I need to put in the actual .module file? I can't seem to find any examples of complete module code that uses this hook to point me in the right direction. Does my module code contain the MODULENAME_views_api() and nothing else?

1 Answer 1

6

If your module doesn't need to implement any other hooks, then yep, hook_views_api() is all you need in your .module file.

As long as you provide the right path in hook_views_api(), Views will know where to look and will dynamically include the files when they're needed.

Usually you need to put files[] = mymodule.FILENAME.inc in your module's .info file for the dynamic code registry to pick it up, but from the looks of it that isn't the case for Views.

If you're looking for some code examples for this, take a look at the Administration Views module; it's .module file contains only hook_views_api() as well (and it works), so you can be confident you're doing the right thing.

7
  • 1
    This is the "proper" thing to do, but I don't recall a case where putting the hooks for altering queries or and of the build phases directly in a .module didn't work.
    – mpdonadio
    Jun 29, 2012 at 17:38
  • @MPD You're right it will always work in a .module file either way. Views just makes sure the file it suggests you put the hook in is included when the hook is invoked
    – Clive
    Jun 29, 2012 at 17:42
  • So I could skip the .inc file entirely and just put in in the .module? My module isn't going to be doing anything other than modifying the query.
    – EmmyS
    Jun 29, 2012 at 17:47
  • Yep, the .module file for any enabled module will be included during the bootstrap phase and views will only ever invoke the hook after that's happened. What I'm not sure of is if views expects the .inc file to be there whether or not it's got code in it. But I think you'll be able to test for that quite easily anyway
    – Clive
    Jun 29, 2012 at 17:52
  • 1
    I'm not really sure to be honest I don't use views all that often, anything I came up with would just be a guess! There are a fair few people who visit this site who know a lot about views programming though so it might be worth asking it as a separate question
    – Clive
    Jun 29, 2012 at 18:48

Your Answer

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

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