Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Where can I learn the views 3 API? I can use the ui to make views but I would like to create views using the API in my modules. I would settle for the views 2 API at this point. I looked at the docs and did not get very far. Anyone know of a good tutorial?

share|improve this question
add comment

4 Answers

Not sure what exactly you mean with "creating" views.

First, if you have custom tables/data, you can use hook_views_data() and custom plugins and handlers to tell views how to access these tables. Some Tutorials/examples: http://treehouseagency.com/blog/neil-hastings/2010/01/19/views-handler-easy-way (Drupal 6) and http://larsolesen.dk/node/273 (Drupal 7). But again, you only need this if you want to expose your own tables to views.

Then, there are a number of pre-render and alter hooks, which allow you to alter some things that you can't do in the user interface. List of views hooks in Drupal 6.

That said, there is no "API" to create actual views (that I know of). I think everybody uses the views UI for that (After all, it's a great UI now in Drupal 7). What you can do is export the views you created in the UI to distribute them with your module or as a new module. This is also useful for contributed/standard modules to not only expose their tables to views but also provide example views, which can be customized/extended for a specific site.

Instead of exporting them manually, you can also use the Features module, which allows you to export all kinds of configuration (views, content types, fields, permissions, ...) into so called features (which is basically an automatically generated module). That is especially useful when moving configuration like views from a development to a production installation, or re-using the same views/configuration on multiple sites.

share|improve this answer
add comment

I was able to make views using Views UI, then I added the exported view into hook_views_default_views() and also created hook_views_api() and it works! This is the best way to do this.

share|improve this answer
add comment

A good way to figure out where to start with the Views 3 API is by looking at the hooks and their execution order. There's a diagram somewhere (unfortunately I can't find it), but this post is a good substitute: It does a good job on explaining the life cycle of a view showing the order in which hook events happen.

share|improve this answer
add comment

Have a look at the book Drupal building blocks from the author of views Earl Miles, it contains both examples with Views 2 and Views 3.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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