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 />

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

$summary = t('Do not show units');<br />

}<br />

}<br />

return $summary;<br />

The form hook is a very simple form offering the user a select box to pick what the<br />

units_as setting should be: column, cell, or none. As with other settings forms, the<br />

name of the form element matches the name of the settings variable so it gets saved<br />

automatically. The summary hook, then, simply takes that setting and returns a<br />

string that <strong>Drupal</strong> can display to the user so that he knows what the current<br />

setting is.<br />

Now let's have a look at the view hook code for the table formatter:<br />

function dimfield_field_formatter_view($obj_type, $object, $field,<br />

$instance, $langcode, $items, $display) {<br />

$element = array();<br />

$settings = $display['settings'];<br />

switch ($display['type']) {<br />

// ...<br />

case 'dimfield_table':<br />

$rows = array();<br />

foreach ($items as $delta => $item) {<br />

$row = array();<br />

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

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

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

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

));<br />

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

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

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

));<br />

}<br />

else {<br />

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

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

}<br />

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

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

[ 203 ]

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

Saved successfully!

Ooh no, something went wrong!