Custom Field Formatters

3,328 views

Published on

How and why to create your own

Published in: Technology, Business
2 Comments
0 Likes
Statistics
Notes
  • Yes, you're right.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • On slide 11, shouldn't the last bullet read:

    theme_MODULE_formatter_MYFORMATTER($element)

    ??
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total views
3,328
On SlideShare
0
From Embeds
0
Number of Embeds
20
Actions
Shares
0
Downloads
35
Comments
2
Likes
0
Embeds 0
No embeds

No notes for slide
















  • Custom Field Formatters

    1. 1. Custom Field Formatters Design for Drupal, June 20, 2010
    2. 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. 3. What is a Field Formatter?
    4. 4. Where are Field Formatters Used • Content Type “Display Options” • Views field options for CCK fields • Panels display options for CCK fields
    5. 5. Those Choices Don’t Cut it ... • When formatter options fail, many start to add complexity to node.tpl.php • Others turn to contemplate module
    6. 6. Devel’s 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. 7. Templating Node Content Breaks Drupal • If adding a new field or module requires you to edit node templates or contemplate, your site is broken
    8. 8. Baby Steps • Exclude certain fields from display, and then output only these separately • Avoid total obliteration of $content
    9. 9. Find the Formatter Theme Functions • Find the closest thing to what you want • Copy it • Tweak it
    10. 10. Start a Custom Module • .info file (meta information) • .module file (hook implementations and other functions)
    11. 11. Functions Needed • hook_field_formatter_info( ) • hook_theme( ) • theme_MODULE_MYFORMATTER_forma tter($element)
    12. 12. hook_field_formatter_info • Provide meta information about your new formatter: name, description, field types • Return an array of formatter(s) keyed by machine names.
    13. 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. 14. Formatter Theme Function • $element is the parameter • $element[‘#item’] contains the meat • dpm to see what you’re working with
    15. 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. 16. Field Formatters in D7 • hook_field_formatter_info includes settings • hook_field_formatter_info_alter ! • hook_field_formatter_view (no theme function)

    ×