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.

Chapter 7<br />

}<br />

'@height' => $item['height'],<br />

'@width' => $item['width'],<br />

'@unit' => dimfield_units($item['units']),<br />

));<br />

}<br />

elseif ($field['settings']['num_dimensions'] == 3) {<br />

$output = t(<br />

'@height @unit by @width @unit by @depth @unit', array(<br />

'@height' => $item['height'],<br />

'@width' => $item['width'],<br />

'@depth' => $item['depth'],<br />

'@unit' => dimfield_units($item['units']),<br />

));<br />

}<br />

$element[$delta] = array('#markup' => $output);<br />

}<br />

break;<br />

}<br />

return $element;<br />

The formatter_view callback is expected to return a renderable array that the<br />

theme system can understand. In this case, we simply want a formatted string that<br />

describes the data stored in the field. As before, we get multiple field values passed<br />

to the callback at once in an array. So we simply iterate over them one by one and<br />

assign them to the $element variable. The #markup element type tells <strong>Drupal</strong> "Here's<br />

some HTML. I've already formatted it, just use it". When that element gets rendered<br />

later, in the page, the strings we generated using the t() function will simply get<br />

displayed with all of the appropriate data in them.<br />

Complex formatters<br />

There is, of course, nothing preventing us from rendering all of the values together if<br />

we want. In fact, our second formatter will do just that. Rather than a series of values<br />

one after another, we'll render all of the available values in a single table.<br />

Then the question arises, how do we display units? As their own column? Inline on<br />

each cell? Just in the header of each dimension column? In cases like this, the best<br />

option is to let the user decide using the configuration capabilities of formatters.<br />

[ 201 ]

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

Saved successfully!

Ooh no, something went wrong!