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.

Creating New Fields<br />

}<br />

$row[] = t('@value (%units)', array(<br />

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

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

));<br />

}<br />

else {<br />

$row[] = $item['depth'];<br />

}<br />

}<br />

if ($settings['units_as'] == 'column') {<br />

$row[] = dimfield_units($item['units']);<br />

}<br />

$rows[] = $row;<br />

$header = array(t('Height'), t('Width'));<br />

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

$header[] = t('Depth');<br />

}<br />

if ($settings['units_as'] == 'column') {<br />

$header[] = t('Units');<br />

}<br />

}<br />

$element = array(<br />

'#theme' => 'table',<br />

'#rows' => $rows,<br />

'#header' => $header,<br />

);<br />

break;<br />

}<br />

return $element;<br />

In this formatter, we build up a series of rows of data consisting of height, width,<br />

and depth if applicable. Each row is an array, and then we have an array of arrays to<br />

give us a table structure. That array we assign to an element that will get rendered<br />

as a table, as defined by the #theme key. Because <strong>Drupal</strong> defines a theme_table()<br />

function, that data will get passed to that function when the element is rendered and<br />

returned as an HTML table.<br />

Note also that we're changing how the table gets built based on the settings we<br />

configured a moment ago. There may or may not be a column dedicated to units,<br />

and we may or may not display the units as part of each cell. Due to the fact that a<br />

settings value is exclusive, we don't need to worry about those two colliding as<br />

both can never be true.<br />

[ 204 ]

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

Saved successfully!

Ooh no, something went wrong!