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

During the validation phase of the form submission, this function will be called<br />

with the element it is attached to (the height_width_depth element) and the<br />

$form_state variable, which is passed by reference so that we can modify it. The<br />

first thing we check is that we're not displaying this widget on the field configuration<br />

page. If so, we don't bother validating it because nothing will be saved anyway.<br />

Then, we check to see how many dimensions we're dealing with since the logic will<br />

be slightly different. We then iterate over each submitted value and, assuming that<br />

it has the requisite × character in it, break up the submitted string into three integers.<br />

The explode() function in PHP will take a string and split it into an array using<br />

the first parameter as a delimiter, while the list() operator will assign that array<br />

to two or three separate variables for us. We then take those values and actively<br />

set the height, width, units, and potential depth values within the form state using<br />

form_set_value().<br />

While it seems odd to use the validation step to manipulate the form data, it is the<br />

only place that the form API allows us to do so. The net result is that we create new<br />

values in the $form_state collection that match up with the columns in our field.<br />

When <strong>Drupal</strong> submits the widget, it will look through the $form_state for variables<br />

that match the names of the columns in the field. It doesn't care that we put those<br />

values there ourselves, just that they exist is what matters. The original string still<br />

exists in the height_width_depth variable, but <strong>Drupal</strong> will just ignore it.<br />

We are also going to do a little custom theming to our combined widget. Note the<br />

following lines:<br />

$element['dimfield_combined_wrapper']['#theme'] = 'dimfield_combined_<br />

wrapper';<br />

$element['dimfield_combined_wrapper']['#attached']['css'][] = drupal_<br />

get_path('module', 'dimfield') . '/dimfield-admin.css';<br />

The first line tells the rendering system to use a theme hook named dimfield_<br />

combined_wrapper to render everything that appears under $element['dimfield_<br />

combined_wrapper']. The second tells the system to also load a particular CSS file<br />

whenever this form element is displayed. In our case we'll do something simple and<br />

just stick the two form elements—height_width_depth and units —into a wrapped<br />

set of divs:<br />

function dimfield_theme() {<br />

return array(<br />

'dimfield_combined_wrapper' => array(<br />

'render element' => 'element',<br />

),<br />

);<br />

[ 198 ]

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

Saved successfully!

Ooh no, something went wrong!