A Gentle Introduction
to the Views API

Dan Muzyka
dan@danmuzyka.com
drupal.org/user/48685
Target Audience
Experience creating views through the UI
Basic working knowledge of PHP
Basic understanding of object-orie...
What we will cover
How to act upon and change Views using Views hooks
Building blocks for creating your own custom fields
...
What we will not cover
How to use the Views UI
How to theme views
Background
Drupal themer and site builder for several years
Module development for about a year
Needed to do highly custom...
Checking Documentation
Better, but without a conceptual
overview I don't understand this.
Much
better!
Goals
Provide conceptual overview
Show where to find documentation
Give tips for searching and deciphering Views code
Illu...
Example Use Case

Zombie Outbreak!
Report of zombies reanimated and destroyed, and
number of brains consumed
Image “Bracki...
Modules Used
Views (Duh!)
CTools
Advanced Help
Devel
Field Collection
Date
Drush
Views API Overview
Views Hooks





Procedural programming
Syntax familiar to
module developers and
to themers who use
...
Good Overview of Views
Hooks and Classes

Chapter 11
Views Hooks
Absolutely, positively essential: hook_views_api()

(This goes in zombies.module)
Views Hooks
Merlinofchaos recommends putting your other hooks in
mymodule.views.inc
Views Hooks
A lot of useful views hooks intercept and change a
view as it's being built
Each of these hooks acts on the vi...
Views Lifecycle Hooks
View Phase

Hook Run Before

Hook Run After

Use

pre_execute()

hook_views_pre_view()

NA

Affect t...
Learning about Views
Lifecycle Hooks

Method 1: Contrib Module API Documentation Sites (e.g.
drupalcontrib.org)






Y...
Learning about Views
Lifecycle Hooks
Learning about Views
Lifecycle Hooks

Method 2: Look at where the hooks are invoked in the
view object


You can find the...
Sample from

views/includes/view.inc
Views hook examples

with Zombies!!!
Views Handlers
Create custom:


Fields



Filters



Sorts



Arguments



Etc.
Adding a Handler


Create the handler file



Make Drupal aware of your handler



Expose your data to your handler


...
Adding a Handler
First, an overview of each step
Then, an example with the zombie view
Creating a Handler File
Find an existing handler similar to what you want to
create
Copy the file with the handler definit...
Creating a Handler File


Change the class name in the handler file



Delete the functions you don't want to override

...
Making Views Aware
of Your Handler

Drupal 6 method: hook_views_handlers()
Making Views Aware
of Your Handler

Drupal 7 method: Load in your module's .info file
Exposing Data

to Your Handler


hook_views_data()






Use to create new handlers (e.g., a new field you can add
thr...
node_views_data() excerpt
hook_views_data()
Documentation

Excellent resource: advanced_help
help/views/api-tables
hook_views_data_alter()
Easier place to start – just override an existing handler
Figuring out what to override
1. Determi...
Slightly Contrived Example:
Custom Date of Destruction Field
Class Inheritance
in Field Example

zombies_handler_field_field_data_field_destruction_date
views_handler_field_field
view...
Handler Method

Documentation

A few commonly-used methods documented in
advanced_help
Drupalcontrib.org: search for
class...
When in doubt, dpm() everything
Less Contrived Example:
Custom Date Filter
Filter Handler Example
Default filter: zombie destruction date >= input date
If the zombie destruction date is NULL (zombi...
Filter Handler Example
Add new check box in admin UI:
'Allow content with an empty date field to pass filter'
Class Inheritance
in Filter Example

zombies_handler_filter_field_destruction_date

date_views_filter_handler_simple

view...
Recap
Views Hooks
Make Views aware of your module: hook_views_api()
Act on your view during the view build process: Views
lifecy...
Views Handlers
Provide custom fields, filters, sorts, arguments,
relationships, etc.
Documentation
advanced_help

Comments and code in the
Views module itself
Documentation
Each of these sources has areas of strength and
weakness; if you don't find a helpful answer in one,
try ano...
General Tips
When in doubt, dpm() everything
grep through the Views directory to find the closest
ancestor class that defi...
Credits
Artwork by Shannon Hayward used under a Creative Commons
Attribution 2.0 license
Brackishfairy of the DEAD:
http:/...
Credits
Miles, E., Miles, L., Hogbin, E. J., & Stevenson, K. (2011). Drupal's
building blocks: Quickly building web sites ...
How to Stalk Me
dan@danmuzyka.com
drupal.org/user/48685
groups.drupal.org/user/8040
@danmuzyka
facebook.com/danmuzyka
link...
Questions?
A Gentle Introduction to Drupal's Views API
Upcoming SlideShare
Loading in …5
×

A Gentle Introduction to Drupal's Views API

6,246 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,246
On SlideShare
0
From Embeds
0
Number of Embeds
68
Actions
Shares
0
Downloads
26
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

A Gentle Introduction to Drupal's Views API

  1. 1. A Gentle Introduction to the Views API Dan Muzyka dan@danmuzyka.com drupal.org/user/48685
  2. 2. Target Audience Experience creating views through the UI Basic working knowledge of PHP Basic understanding of object-oriented PHP helpful, not required No Views development experience
  3. 3. What we will cover How to act upon and change Views using Views hooks Building blocks for creating your own custom fields and filters (and by extrapolation, your own custom sorts, arguments, etc.)
  4. 4. What we will not cover How to use the Views UI How to theme views
  5. 5. Background Drupal themer and site builder for several years Module development for about a year Needed to do highly custom views for a client
  6. 6. Checking Documentation
  7. 7. Better, but without a conceptual overview I don't understand this.
  8. 8. Much better!
  9. 9. Goals Provide conceptual overview Show where to find documentation Give tips for searching and deciphering Views code Illustrate the concepts with examples Equip you to learn more on your own Strike a balance between clarity and not insulting your intelligence
  10. 10. Example Use Case Zombie Outbreak! Report of zombies reanimated and destroyed, and number of brains consumed Image “Brackishfairy of the DEAD” by Shannon Hayward used under a Creative Commons Attribution 2.0 License http://www.flickr.com/photos/sketchink/2314369576/
  11. 11. Modules Used Views (Duh!) CTools Advanced Help Devel Field Collection Date Drush
  12. 12. Views API Overview Views Hooks    Procedural programming Syntax familiar to module developers and to themers who use preprocess functions Useful for simple overrides and for exposing data to Views classes Views Classes and Objects   Object-oriented PHP Useful for creating custom fields, filters, sorts, arguments, relationships, etc.
  13. 13. Good Overview of Views Hooks and Classes Chapter 11
  14. 14. Views Hooks Absolutely, positively essential: hook_views_api() (This goes in zombies.module)
  15. 15. Views Hooks Merlinofchaos recommends putting your other hooks in mymodule.views.inc
  16. 16. Views Hooks A lot of useful views hooks intercept and change a view as it's being built Each of these hooks acts on the view at a particular stage of the 'Views Lifecycle'
  17. 17. Views Lifecycle Hooks View Phase Hook Run Before Hook Run After Use pre_execute() hook_views_pre_view() NA Affect the view early in its lifecycle build() hook_views_pre_build() hook_views_post_build() Affect the view immediately before or after it builds the query execute() hook_views_pre_execute() hook_views_post_execute() Affect the view immediately before or after the query runs render() hook_views_pre_render() (Themes can also use this hook) hook_views_post_render() Affect the view immediately before or (Themes can also use this hook) after the HTML markup is generated
  18. 18. Learning about Views Lifecycle Hooks Method 1: Contrib Module API Documentation Sites (e.g. drupalcontrib.org)    Yes, you get SOME information by searching for hook_views_pre_build, hook_views_pre_execute, etc. BUT, you get more context by seeing where these hooks are invoked in the view build process Search for:  view::pre_execute  view::build  view::execute  view::render
  19. 19. Learning about Views Lifecycle Hooks
  20. 20. Learning about Views Lifecycle Hooks Method 2: Look at where the hooks are invoked in the view object  You can find the $view->pre_execute(), $view->build(), $view->execute(), and $view->render() methods in: views/includes/view.inc
  21. 21. Sample from views/includes/view.inc
  22. 22. Views hook examples with Zombies!!!
  23. 23. Views Handlers Create custom:  Fields  Filters  Sorts  Arguments  Etc.
  24. 24. Adding a Handler  Create the handler file  Make Drupal aware of your handler  Expose your data to your handler  Clear cache!
  25. 25. Adding a Handler First, an overview of each step Then, an example with the zombie view
  26. 26. Creating a Handler File Find an existing handler similar to what you want to create Copy the file with the handler definition into your module directory Rename the handler file merlinofchaos recommends this naming convention: [module]_handler_[type]_[tablename]_[fieldname] (You don't actually have to be this verbose)
  27. 27. Creating a Handler File  Change the class name in the handler file  Delete the functions you don't want to override  Edit the functions you DO want to override
  28. 28. Making Views Aware of Your Handler Drupal 6 method: hook_views_handlers()
  29. 29. Making Views Aware of Your Handler Drupal 7 method: Load in your module's .info file
  30. 30. Exposing Data to Your Handler  hook_views_data()    Use to create new handlers (e.g., a new field you can add through the Views UI) Expose data from the database that is not already exposed to Views hook_views_data_alter()   Use to force existing fields, filters, etc to load your handler instead of the existing handler -orChange the data exposed to the existing handler
  31. 31. node_views_data() excerpt
  32. 32. hook_views_data() Documentation Excellent resource: advanced_help help/views/api-tables
  33. 33. hook_views_data_alter() Easier place to start – just override an existing handler Figuring out what to override 1. Determine the existing handler: dpm($view) inside one of the Views lifecycle hooks 2. Determine where in the $data array to put your override: dpm($data) in hook_views_data_alter() and clear cache
  34. 34. Slightly Contrived Example: Custom Date of Destruction Field
  35. 35. Class Inheritance in Field Example zombies_handler_field_field_data_field_destruction_date views_handler_field_field views_handler_field views_handler views_object
  36. 36. Handler Method Documentation A few commonly-used methods documented in advanced_help Drupalcontrib.org: search for class_name::method_name (e.g. views_handler_field_field::render_items) Comments in the code itself
  37. 37. When in doubt, dpm() everything
  38. 38. Less Contrived Example: Custom Date Filter
  39. 39. Filter Handler Example Default filter: zombie destruction date >= input date If the zombie destruction date is NULL (zombie has not been destroyed, yet), this does not give us result we want
  40. 40. Filter Handler Example Add new check box in admin UI: 'Allow content with an empty date field to pass filter'
  41. 41. Class Inheritance in Filter Example zombies_handler_filter_field_destruction_date date_views_filter_handler_simple views_handler_filter_date views_handler_filter_numeric views_handler_filter views_handler views_object
  42. 42. Recap
  43. 43. Views Hooks Make Views aware of your module: hook_views_api() Act on your view during the view build process: Views lifecycle hooks Provide data to your handlers: hook_views_data() and hook_views_data_alter()
  44. 44. Views Handlers Provide custom fields, filters, sorts, arguments, relationships, etc.
  45. 45. Documentation advanced_help Comments and code in the Views module itself
  46. 46. Documentation Each of these sources has areas of strength and weakness; if you don't find a helpful answer in one, try another
  47. 47. General Tips When in doubt, dpm() everything grep through the Views directory to find the closest ancestor class that defines the function you want to override
  48. 48. Credits Artwork by Shannon Hayward used under a Creative Commons Attribution 2.0 license Brackishfairy of the DEAD: http://www.flickr.com/photos/sketchink/2314369576/ JASON of the DEAD: http://www.flickr.com/photos/sketchink/2776234924/ RANDI of the DEAD: http://www.flickr.com/photos/sketchink/2776238718/ Untitled: http://www.flickr.com/photos/sketchink/5476952538/ Untitled: http://www.flickr.com/photos/sketchink/5476953706/
  49. 49. Credits Miles, E., Miles, L., Hogbin, E. J., & Stevenson, K. (2011). Drupal's building blocks: Quickly building web sites with CCK, Views, and Panels. Upper Saddle River, NJ: Addison-Wesley.
  50. 50. How to Stalk Me dan@danmuzyka.com drupal.org/user/48685 groups.drupal.org/user/8040 @danmuzyka facebook.com/danmuzyka linkedin.com/in/danmuzyka IRC: danmuzyka
  51. 51. Questions?

×