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

Recall from a moment ago that the dimfield_table formatter declared a settings<br />

key, which was an array. That array defines all of the possible settings parameters<br />

for that formatter and their default values. In order to make use of formatter settings<br />

there are also two other hooks we need to implement: hook_field_formatter_<br />

settings_summary() and hook_field_formatter_settings_form().<br />

function dimfield_field_formatter_settings_form($field, $instance,<br />

$view_mode, $form, &$form_state) {<br />

$display = $instance['display'][$view_mode];<br />

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

$form = array();<br />

if ($display['type'] == 'dimfield_table') {<br />

$form['units_as'] = array(<br />

'#title' => t('Show units'),<br />

'#type' => 'select',<br />

'#options' => array(<br />

'column' => t('As their own column'),<br />

'cell' => t('In each cell'),<br />

'none' => t('Do not show units'),<br />

),<br />

'#default_value' => $settings['units_as'],<br />

'#required' => TRUE,<br />

);<br />

}<br />

return $form;<br />

}<br />

function dimfield_field_formatter_settings_summary($field, $instance,<br />

$view_mode) {<br />

$display = $instance['display'][$view_mode];<br />

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

$summary = '';<br />

if ($display['type'] == 'dimfield_table') {<br />

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

$summary = t('Show units as their own column');<br />

}<br />

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

$summary = t('Show units in each cell');<br />

}<br />

[ 202 ]

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

Saved successfully!

Ooh no, something went wrong!