This document provides an introduction to customizing views using Views hooks and handlers in Drupal. It begins with an overview of the target audience and goals. It then covers Views hooks, which allow modifying views at different stages of processing, and Views handlers, which allow creating custom fields, filters, and other components. Examples are provided for a custom date filter handler and date field handler related to tracking zombies. The document concludes with tips on documentation and contacting the author.
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. 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. What we will not cover
How to use the Views UI
How to theme views
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
10. 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
11. 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/
13. 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.
17. 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'
18. 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
19. 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
21. 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
27. 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)
28. 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
30. Making Views Aware
of Your Handler
Drupal 7 method: Load in your module's .info file
31. 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
34. 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
36. Class Inheritance
in Field Example
zombies_handler_field_field_data_field_destruction_date
views_handler_field_field
views_handler_field
views_handler
views_object
37. 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
40. 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
41. Filter Handler Example
Add new check box in admin UI:
'Allow content with an empty date field to pass filter'
42. 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
44. 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()
47. Documentation
Each of these sources has areas of strength and
weakness; if you don't find a helpful answer in one,
try another
48. 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
49. 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/
50. 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.
51. 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