18.10.2016 Views

Drupal 7 Module Development

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Creating New Fields<br />

In the last chapter we saw how to define new entities. The main advantage of<br />

defining our content as an entity, of course, was to make it accessible to the Field<br />

API. In this chapter, we will look at how fields work and how to define new fields<br />

to attach to either our own entities or to those already defined, such as nodes.<br />

Along the way we'll cover:<br />

• What Field API is and what it does<br />

• How to use magic callbacks<br />

• Using fields to store data<br />

• Using widgets to hook fields into the Form API<br />

• Using formatters to control the display of fields<br />

When we're done, you should have an understanding of how all the key pieces of<br />

<strong>Drupal</strong>'s main data handling system fit together and how to leverage them best.<br />

Our goal: a "dimensions" field<br />

In the last chapter, we created a new type of entity called Artwork. As a fieldable<br />

entity, we can attach any field we want to it. Sometimes though, we'll still need to<br />

create our own field types. That may happen if we want to treat a given piece of<br />

information atomically rather than building it up out of smaller parts. There are<br />

many reasons why that could be the case. They are as follows:<br />

1. We want to conceptually treat that piece of data as a single chunk with<br />

its own meaning rather than as a series of chunks that together we know<br />

has meaning.<br />

2. We have complex data but want to have multiple instances of that data on a<br />

single entity.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!