52
A Gentle Introduction to the Views API Dan Muzyka [email protected] drupal.org/user/48685

A Gentle Introduction to Drupal's Views API

Embed Size (px)

Citation preview

Page 1: A Gentle Introduction to Drupal's Views API

A Gentle Introduction to the Views API

Dan [email protected]

drupal.org/user/48685

Page 2: A Gentle Introduction to Drupal's Views API

Target AudienceExperience creating views through the UI

Basic working knowledge of PHP

Basic understanding of object-oriented PHP helpful, not required

No Views development experience

Page 3: A Gentle Introduction to Drupal's Views API

What we will coverHow 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.)

Page 4: A Gentle Introduction to Drupal's Views API

What we will not coverHow to use the Views UI

How to theme views

Page 5: A Gentle Introduction to Drupal's Views API

BackgroundDrupal themer and site builder for several years

Module development for about a year

Needed to do highly custom views for a client

Page 6: A Gentle Introduction to Drupal's Views API

Checking Documentation

Page 7: A Gentle Introduction to Drupal's Views API
Page 8: A Gentle Introduction to Drupal's Views API

Better, but without a conceptual overview I don't understand this.

Page 9: A Gentle Introduction to Drupal's Views API

Much better!

Page 10: A Gentle Introduction to Drupal's Views API

GoalsProvide 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

Page 11: A Gentle Introduction to Drupal's Views API

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/

Page 12: A Gentle Introduction to Drupal's Views API

Modules UsedViews (Duh!)

CTools

Advanced Help

Devel

Field Collection

Date

Drush

Page 13: A Gentle Introduction to Drupal's Views API

Views API OverviewViews 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.

Page 14: A Gentle Introduction to Drupal's Views API

Good Overview of Views Hooks and Classes

Chapter 11

Page 15: A Gentle Introduction to Drupal's Views API

Views HooksAbsolutely, positively essential: hook_views_api()

(This goes in zombies.module)

Page 16: A Gentle Introduction to Drupal's Views API

Views HooksMerlinofchaos recommends putting your other hooks in

mymodule.views.inc

Page 17: A Gentle Introduction to Drupal's Views API

Views HooksA 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'

Page 18: A Gentle Introduction to Drupal's Views API

Views Lifecycle HooksView Phase Hook Run Before Hook Run After Usepre_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()(Themes can also use this hook)

Affect the view immediately before or after the HTML markup is generated

Page 19: A Gentle Introduction to Drupal's Views API

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

Page 20: A Gentle Introduction to Drupal's Views API

Learning about Views Lifecycle Hooks

Page 21: A Gentle Introduction to Drupal's Views API

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

Page 22: A Gentle Introduction to Drupal's Views API

Sample fromviews/includes/view.inc

Page 23: A Gentle Introduction to Drupal's Views API

Views hook exampleswith Zombies!!!

Page 24: A Gentle Introduction to Drupal's Views API

Views HandlersCreate custom:

Fields Filters Sorts Arguments Etc.

Page 25: A Gentle Introduction to Drupal's Views API

Adding a Handler Create the handler file Make Drupal aware of your handler Expose your data to your handler Clear cache!

Page 26: A Gentle Introduction to Drupal's Views API

Adding a HandlerFirst, an overview of each step

Then, an example with the zombie view

Page 27: A Gentle Introduction to Drupal's Views API

Creating a Handler FileFind an existing handler similar to what you want to

create

Copy the file with the handler definition into your module directory

Rename the handler filemerlinofchaos recommends this naming convention:

[module]_handler_[type]_[tablename]_[fieldname]

(You don't actually have to be this verbose)

Page 28: A Gentle Introduction to Drupal's Views API

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

Page 29: A Gentle Introduction to Drupal's Views API

Making Views Aware of Your Handler

Drupal 6 method: hook_views_handlers()

Page 30: A Gentle Introduction to Drupal's Views API

Making Views Aware of Your Handler

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

Page 31: A Gentle Introduction to Drupal's Views API

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 -or-

Change the data exposed to the existing handler

Page 32: A Gentle Introduction to Drupal's Views API

node_views_data() excerpt

Page 33: A Gentle Introduction to Drupal's Views API

hook_views_data() Documentation

Excellent resource: advanced_help

help/views/api-tables

Page 34: A Gentle Introduction to Drupal's Views API

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

Page 35: A Gentle Introduction to Drupal's Views API

Slightly Contrived Example:Custom Date of Destruction Field

Page 36: A Gentle Introduction to Drupal's Views API

Class Inheritancein Field Example

zombies_handler_field_field_data_field_destruction_date

views_handler_field_field

views_handler_field

views_handler

views_object

Page 37: A Gentle Introduction to Drupal's Views API

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

Page 38: A Gentle Introduction to Drupal's Views API

When in doubt, dpm() everything

Page 39: A Gentle Introduction to Drupal's Views API

Less Contrived Example:Custom Date Filter

Page 40: A Gentle Introduction to Drupal's Views API

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

Page 41: A Gentle Introduction to Drupal's Views API

Filter Handler Example

Add new check box in admin UI:

'Allow content with an empty date field to pass filter'

Page 42: A Gentle Introduction to Drupal's Views API

Class Inheritancein Filter Examplezombies_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

Page 43: A Gentle Introduction to Drupal's Views API

Recap

Page 44: A Gentle Introduction to Drupal's Views API

Views HooksMake 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()

Page 45: A Gentle Introduction to Drupal's Views API

Views HandlersProvide custom fields, filters, sorts, arguments,

relationships, etc.

Page 46: A Gentle Introduction to Drupal's Views API

Documentation

advanced_help

Comments and code in the Views module itself

Page 47: A Gentle Introduction to Drupal's Views API

DocumentationEach of these sources has areas of strength and

weakness; if you don't find a helpful answer in one, try another

Page 48: A Gentle Introduction to Drupal's Views API

General TipsWhen in doubt, dpm() everything

grep through the Views directory to find the closest ancestor class that defines the function you want to override

Page 49: A Gentle Introduction to Drupal's Views API

CreditsArtwork 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/

Page 50: A Gentle Introduction to Drupal's Views API

CreditsMiles, 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.

Page 51: A Gentle Introduction to Drupal's Views API

How to Stalk [email protected]

drupal.org/user/48685

groups.drupal.org/user/8040

@danmuzyka

facebook.com/danmuzyka

linkedin.com/in/danmuzyka

IRC: danmuzyka

Page 52: A Gentle Introduction to Drupal's Views API

Questions?