Book Image

Drupal 7 Module Development

By : Matt Butcher, Larry Garfield, John Wilkins, Matt Farina, Ken Rickard, Greg Dunlap
Book Image

Drupal 7 Module Development

By: Matt Butcher, Larry Garfield, John Wilkins, Matt Farina, Ken Rickard, Greg Dunlap

Overview of this book

<p>Drupal is an award-winning open-source Content Management System. It's a modular system, with an elegant hook-based architecture, and great code. Modules are plugins for Drupal that extend, build or enhance Drupal core functionality.</p> <p>In Drupal 7 Module development book, six professional Drupal developers use a practical, example-based approach to introduce PHP developers to the powerful new Drupal 7 tools, APIs, and strategies for writing custom Drupal code.</p> <p>These tools not only make management and maintenance of websites much easier, but they are also great fun to play around with and amazingly easy to use.<br /><br />If you're eager to learn about these new APIs and start creating modules for Drupal 7, this is your book. Walk through the development of complete Drupal modules with this primer for PHP programmers.</p> <p>From basic modules and themes to sophisticated Drupal extensions, learn how to use Drupal's API and hook system to build powerful custom Drupal websites. With an emphasis on practical programming, this book takes a project-based approach, providing working examples in every chapter<br />Specifically written for Drupal 7, this book will get you coding modules as quickly as possible, and help you add the features that will give your work that professional gloss!<br /><br />This book will walk you through the development of complete Drupal modules and show you how to add various features to meet your requirements.</p> <p>The Drupal content management system, written in the popular PHP language, has skyrocketed in popularity.<br /><br />Developers for this system are in high demand. This book prepares PHP developers for Drupal development, explaining architecture, exploring APIs, and emphasizing practical approaches.</p> <p>In each chapter, readers will learn new aspects of the system while creating fully-functioning modules, themes, and libraries. Learn how to “hook into” any part of the Drupal process, creating custom content types, extending existing capabilities, and integrating with external services and applications.</p>
Table of Contents (22 chapters)
Drupal 7 Module Development
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Using formatters to display our field


Now that we've defined our field type, and we've created a widget to make it editable from a form, the only piece left is to decide how to display it in user output. (User output usually means the computer screen, but it could also mean an RSS feed, printed page, or various other types of output). Drupal lets us control that display using formatters.

Formatters follow a very similar pattern to field types and widgets. There is an info hook to define what formatters are available, and then there's a series of callbacks for all of the formatters our module defines. In most cases though, there's only one callback we need worry about.

Declaring a formatter

First, let's look at the info hook given here:

function dimfield_field_formatter_info() {
  return array(
    'dimfield_default' => array(
      'label' => t('Default'),
      'field types' => array('dimensions'),
    ),
    'dimfield_table' => array(
      'label' => t('Show as table'),
  ...