You are on page 1of 26

Use views programmatically

Sorin Dediu Drupal developer

When we should override our view


Some time when you have a non standard situation. You need to output views results in a way that Views cant natively. You have an exposed filter and it not work like you need. You need to override a view query. Something else.

Let's start, but how?

hook_views_api()
The first thing you have to do is tell Views that your module is going to using the Views API

hook_views_api()
Next we creating new file with name: mymodule.views.inc and it should be placed in our current module: includes/views/
This line tells views to look in /includes/views/ and all hooks from this file will be auto-loaded by views module.

hook_views_api()

What we can use in this file ?

hook_views_api()

You can use here all hooks provided by views core module.
http://api.drupal.org/api/views/views.api.php/

Lets see an easy way to call a view programmatically

If you want to send an argument into the views contextual filter, simply add another argument to the views_embed_view function.

Lets see an easy way to call a view programmatically

Here can be any other argument, nid, title, date for example: if you use a date as a contextual filter the allowed type of format date is YYYY-MM-DD.

Small Tip
Why we should to rename views display machine name ?

Small Tip
Will help you with naming views template files Help developers when using views programmatically Help site maintainers to maintain a view with multiple displays Help themer to easy theme the view

views_get_view()
Get a view from the database or from default views. This function is just a static wrapper around views::load(). This function isn't called 'views_load()' primarily because it might get a view from the default views which aren't technically loaded from the database.

Calling the View


Just call the view to make sure it work

Calling the View


By default, views_get_view() returns the default display, but we need to return the block display. In this case, the display that we have is the first block, so its key was block_1.

Filtering Results
Lets add a filter and filtering rows based on the $delta value. Views prefers the date in a machine readable format, according to the UI.

hook_views_query_alter()

Allow to alter the query before executing the query. To pass

variables from exposed filters in query or join new table to the views query.

hook_views_query_alter()

hook_views_pre_view()
Allows altering a view at the very beginning of views processing, before anything is done. You can change here view $display_id or override view arguments.

hook_views_pre_view()
Small example:

hook_views_pre_render()
This hook is called right before the render process. The query has been executed, and the pre_render() phase has already happened for handlers, so all data should be available.

hook_views_pre_render()

hook_views_default_views()
This hook allows modules to provide their own views which can either be used as-is or as a "starter" for users to build from.This hook should be placed in MODULENAME.views_default.inc

hook_views_default_views()
You just need to add your exported view here and it will be used as default.

Want to see more ? Try it yourself ;)


http://api.drupal.org/api/views/views.api.php/

Questions ?

Thanks you

sorin.dediu@wearepropeople.com

You might also like