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

$element['width'] = array(<br />

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

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

'#default_value' => isset($items[$delta]['width']) ?<br />

$items[$delta]['width'] : NULL,<br />

) + $base;<br />

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

$element['depth'] = array(<br />

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

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

'#default_value' => isset($items[$delta]['depth']) ?<br />

$items[$delta]['depth'] : NULL,<br />

) + $base;<br />

}<br />

}<br />

$element['units'] = array(<br />

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

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

'#default_value' => isset($items[$delta]['units']) ?<br />

$items[$delta]['units'] : NULL,<br />

'#options' => dimfield_units(),<br />

);<br />

}<br />

return $element;<br />

Once again, notice that we're checking for which widget we are using in this<br />

callback, since both widgets will use the same callback. Our parameters include<br />

the form that this widget will be added to and its $form_state. Although they are<br />

passed by reference, we will not actually be modifying them directly (most of the<br />

time). Instead, we will return an $element Form API fragment that <strong>Drupal</strong> will<br />

insert into the form in the correct place. The $element that is passed in contains basic<br />

information about the widget itself, which we will store in our own variable to pass<br />

forward. The Form API will ignore properties it doesn't recognize, but that data will<br />

be available to us later.<br />

[ 193 ]

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

Saved successfully!

Ooh no, something went wrong!