SlideShare a Scribd company logo
Matthew Wrather
http://wrathercreative.com • @mwrather
Sponsored by Exaltation of Larks, Inc.
http://larks.la • @LarksLA
GLAD High Performance Drupal Meetup
Hosted by Filter Digital
June 18, 2013 • Culver City, CA
Views Style Plugins
A Crash Course in
Drupal Hooks
• In Drupal, you take advantage of hooks via
function naming conventions, not by
registering a function with a listener.
• mymodule_foo() implements hook_foo()
• module_invoke_all($hook[, $args])
Returns an array of return values from each
of the hook implementations.
Views Plugins
• Display Plugins
How is your view exposed to other parts
of Drupal? (e.g.,“Page” and “Block”).
• Style Plugins
How is the view displayed? (e.g., <ul>,
<table>)
• And many more
Documentation: http://bit.ly/10MCs3j
Views Style Plugins
Four Steps to Plug In
• Implement hook_views_api()
• Implement hook_views_plugin()
• Extend the appropriate plugin class
• (optional) Theme output
Module Structure
hook_views_api()
hook_views_plugins()
• Describes plugins defined by the module.
• This hook should be placed in the file MODULENAME.views.inc
and it will be auto-loaded.
• MODULENAME.views.inc must be in the directory specified by
the 'path' key returned by MODULENAME_views_api(), or the
same directory as the .module file, if 'path' is unspecified.
• All plugin files need to be referenced in MODULENAME.info with
the files[] directive.
• http://bit.ly/11SfM35
Views Style Plugins
Views Style Plugins
So let’s recap…
• We’ve implemented hook_views_api() in the
MODULENAME.module file
• We put MODULENAME.views.inc either alongside the
MODULENAME.module file or else in the directory
defined in the path key in MODULENAME_views_api()
• We’ve implemented hook_views_plugins() in the
MODULENAME.views.inc file
• …and we still haven’t written the plugin.
class views_plugin_style
• Remember, this is referenced in the
MODULENAME.info file
• This is the actual implementation of the views style,
and controls the options, options form, theme
functions, and rendering of the view.
• N.B.: the file needs to be named the same as the class
it defines with the .inc extention
• http://bit.ly/15w64lo
Views Style Plugins
Views Style Plugins
template_preprocess_
HOOKNAME(&$vars)
• In Drupal, theme preprocess functions set up the variables
that will be rendered in a .tpl.php or template file.
• (Cf. theme_ functions, which output HTML without
template files.)
• The template_ prefix tells Drupal to execute this function
before any other preprocess functions.
• Preprocess functions are hookable, so your module or
theme can override them following the convention
MODULENAME_preprocess_HOOKNAME(&$vars)
THEMENAME_preprocess_HOOKNAME(&$vars)
TEMPLATE.tpl.php
• Contains the actual HTML.
• Shouldn’t contain logic beyond basic flow
control:
if (){…} and foreach (){…}
• That said, you gotta do wahtcha gotta do
to balance the readability of the template
with the larger goal of separating
the business logic from the presentation layer.
Design Decisions & Tradeoffs
• The structure of the Drupal andViews APIs enforces
certain good coding practices like separation of concerns.
• Code organization:What goes where?
(By convention, you put all theViews stuff in views/).
• Validating and Sanitizing user input is crucial, especially in a
module like this one, whose point is displaying data. But the
good news is you get a lot of that for free from Drupal.
• Departures from best practices may seem to increase code
readability in the short term, but you’re going to have to lie
in the bed you’ve made…better to do it “The Drupal Way.”
Questions?
Let me ask my friend Google.
Matthew Wrather • @mwrather • wrathercreative.com

More Related Content

Views Style Plugins

  • 1. Matthew Wrather http://wrathercreative.com • @mwrather Sponsored by Exaltation of Larks, Inc. http://larks.la • @LarksLA GLAD High Performance Drupal Meetup Hosted by Filter Digital June 18, 2013 • Culver City, CA Views Style Plugins
  • 2. A Crash Course in Drupal Hooks • In Drupal, you take advantage of hooks via function naming conventions, not by registering a function with a listener. • mymodule_foo() implements hook_foo() • module_invoke_all($hook[, $args]) Returns an array of return values from each of the hook implementations.
  • 3. Views Plugins • Display Plugins How is your view exposed to other parts of Drupal? (e.g.,“Page” and “Block”). • Style Plugins How is the view displayed? (e.g., <ul>, <table>) • And many more Documentation: http://bit.ly/10MCs3j
  • 5. Four Steps to Plug In • Implement hook_views_api() • Implement hook_views_plugin() • Extend the appropriate plugin class • (optional) Theme output
  • 8. hook_views_plugins() • Describes plugins defined by the module. • This hook should be placed in the file MODULENAME.views.inc and it will be auto-loaded. • MODULENAME.views.inc must be in the directory specified by the 'path' key returned by MODULENAME_views_api(), or the same directory as the .module file, if 'path' is unspecified. • All plugin files need to be referenced in MODULENAME.info with the files[] directive. • http://bit.ly/11SfM35
  • 11. So let’s recap… • We’ve implemented hook_views_api() in the MODULENAME.module file • We put MODULENAME.views.inc either alongside the MODULENAME.module file or else in the directory defined in the path key in MODULENAME_views_api() • We’ve implemented hook_views_plugins() in the MODULENAME.views.inc file • …and we still haven’t written the plugin.
  • 12. class views_plugin_style • Remember, this is referenced in the MODULENAME.info file • This is the actual implementation of the views style, and controls the options, options form, theme functions, and rendering of the view. • N.B.: the file needs to be named the same as the class it defines with the .inc extention • http://bit.ly/15w64lo
  • 15. template_preprocess_ HOOKNAME(&$vars) • In Drupal, theme preprocess functions set up the variables that will be rendered in a .tpl.php or template file. • (Cf. theme_ functions, which output HTML without template files.) • The template_ prefix tells Drupal to execute this function before any other preprocess functions. • Preprocess functions are hookable, so your module or theme can override them following the convention MODULENAME_preprocess_HOOKNAME(&$vars) THEMENAME_preprocess_HOOKNAME(&$vars)
  • 16. TEMPLATE.tpl.php • Contains the actual HTML. • Shouldn’t contain logic beyond basic flow control: if (){…} and foreach (){…} • That said, you gotta do wahtcha gotta do to balance the readability of the template with the larger goal of separating the business logic from the presentation layer.
  • 17. Design Decisions & Tradeoffs • The structure of the Drupal andViews APIs enforces certain good coding practices like separation of concerns. • Code organization:What goes where? (By convention, you put all theViews stuff in views/). • Validating and Sanitizing user input is crucial, especially in a module like this one, whose point is displaying data. But the good news is you get a lot of that for free from Drupal. • Departures from best practices may seem to increase code readability in the short term, but you’re going to have to lie in the bed you’ve made…better to do it “The Drupal Way.”
  • 18. Questions? Let me ask my friend Google. Matthew Wrather • @mwrather • wrathercreative.com