際際滷

際際滷Share a Scribd company logo
Custom Field Formatters




  Design for Drupal, June 20, 2010
Jody Hamilton

 Owner, Lead Developer at Zivtech
 Drupal architect, developer, themer, site-
  builder, project manager, teacher and
  student
 Jody Lynn on drupal.org
What is a Field
 Formatter?
Where are Field
     Formatters Used

 Content Type Display Options
 Views 鍖eld options for CCK 鍖elds
 Panels display options for CCK 鍖elds
Those Choices Dont
       Cut it ...

 When formatter options fail, many start to
  add complexity to node.tpl.php
 Others turn to contemplate module
Devels Node Tabs

 http://drupal.org/project/devel
 Browse the raw data of the loaded and
  rendered node
 dpm( ): a devel function to view data
Templating Node
Content Breaks Drupal

 If adding a new 鍖eld or module requires
  you to edit node templates or contemplate,
  your site is broken
Baby Steps


 Exclude certain 鍖elds from display, and then
  output only these separately
 Avoid total obliteration of $content
Find the Formatter
    Theme Functions

 Find the closest thing to what you want
 Copy it
 Tweak it
Start a Custom Module


 .info 鍖le (meta information)
 .module 鍖le (hook implementations and
  other functions)
Functions Needed

 hook_鍖eld_formatter_info( )
 hook_theme( )
 theme_MODULE_MYFORMATTER_forma
 tter($element)
hook_鍖eld_formatter_info


 Provide meta information about your new
  formatter: name, description, 鍖eld types
 Return an array of formatter(s) keyed by
  machine names.
hook_theme

 Describe your new theme function (or
  template) to Drupal
 You must clear the theme registry when
  adding/changing a hook_theme
  implementation
Formatter Theme
        Function

 $element is the parameter
 $element[#item] contains the meat
 dpm to see what youre working with
Sanitize your Output

 The responsibility for sanitizing user-
  generated data falls on the themers and
  developers who output it with code.

 When in doubt: check_plain or
  check_markup
Field Formatters in D7

 hook_鍖eld_formatter_info includes settings
 hook_鍖eld_formatter_info_alter !
 hook_鍖eld_formatter_view (no theme
  function)

More Related Content

Custom Field Formatters

  • 1. Custom Field Formatters Design for Drupal, June 20, 2010
  • 2. Jody Hamilton Owner, Lead Developer at Zivtech Drupal architect, developer, themer, site- builder, project manager, teacher and student Jody Lynn on drupal.org
  • 3. What is a Field Formatter?
  • 4. Where are Field Formatters Used Content Type Display Options Views 鍖eld options for CCK 鍖elds Panels display options for CCK 鍖elds
  • 5. Those Choices Dont Cut it ... When formatter options fail, many start to add complexity to node.tpl.php Others turn to contemplate module
  • 6. Devels Node Tabs http://drupal.org/project/devel Browse the raw data of the loaded and rendered node dpm( ): a devel function to view data
  • 7. Templating Node Content Breaks Drupal If adding a new 鍖eld or module requires you to edit node templates or contemplate, your site is broken
  • 8. Baby Steps Exclude certain 鍖elds from display, and then output only these separately Avoid total obliteration of $content
  • 9. Find the Formatter Theme Functions Find the closest thing to what you want Copy it Tweak it
  • 10. Start a Custom Module .info 鍖le (meta information) .module 鍖le (hook implementations and other functions)
  • 11. Functions Needed hook_鍖eld_formatter_info( ) hook_theme( ) theme_MODULE_MYFORMATTER_forma tter($element)
  • 12. hook_鍖eld_formatter_info Provide meta information about your new formatter: name, description, 鍖eld types Return an array of formatter(s) keyed by machine names.
  • 13. hook_theme Describe your new theme function (or template) to Drupal You must clear the theme registry when adding/changing a hook_theme implementation
  • 14. Formatter Theme Function $element is the parameter $element[#item] contains the meat dpm to see what youre working with
  • 15. Sanitize your Output The responsibility for sanitizing user- generated data falls on the themers and developers who output it with code. When in doubt: check_plain or check_markup
  • 16. Field Formatters in D7 hook_鍖eld_formatter_info includes settings hook_鍖eld_formatter_info_alter ! hook_鍖eld_formatter_view (no theme function)