18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7<br />

We can, of course, have much more complex formatters. To use our map example we<br />

have seen previously, a formatter could take a series of coordinate data and output<br />

them on a map using a mapping service. As long as we return a renderable array<br />

to <strong>Drupal</strong>, we can do whatever we want.<br />

Managing non-Field fields<br />

One of the advantages of using the Field system is that it gives us a consistent,<br />

powerful UI for all Field data that we add to an entity. However, an entity may have<br />

non-Field data on it as well. A node or artwork title, for instance, is not a Field. The<br />

poll module in core doesn't use Fields for the poll configuration. An entity that is<br />

being pulled from a third party system may have all sorts of data associated with it<br />

that does not live in a Field module.<br />

Fortunately, <strong>Drupal</strong> offers a way for us to integrate that data into the Field UI. Let's<br />

go back to the artwork module from Chapter 6, Working with Content and integrate<br />

the title into the Field system. It takes but a single hook:<br />

function artwork_field_extra_fields() {<br />

$extra = array();<br />

foreach (artwork_types() as $type) {<br />

$extra['artwork'][$type->type] = array(<br />

'form' => array(<br />

'title' => array(<br />

'label' => t('Title'),<br />

'description' => t('The name of the artwork'),<br />

'weight' => -5,<br />

),<br />

),<br />

'display' => array(<br />

'title' => array(<br />

'label' => t('Title'),<br />

'description' => t('The name of the artwork'),<br />

'weight' => -5,<br />

),<br />

),<br />

);<br />

}<br />

return $extra;<br />

}<br />

[ 205 ]

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

Saved successfully!

Ooh no, something went wrong!