I've been upgrading a module and as the documentation says might happen, forms is becoming my biggest step.

I have one question so far. One of my conversions when using the forms updater might typically look like this:

$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => $edit['name'],
'#size' => 30,
'#maxlength' => 255,
);

When looking through the Drupal modules sometimes I'll see a similar structure without the blank field on the end so it will look something like this instead:

$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => $edit['name'],
'#size' => 30,
'#maxlength' => 255); Note there is no comma after the 255 on this version.

My question is: when is it okay to leave the comma off the end and why?

I'm asking this in the context of both creating a form and in the context of the following documentation:

"...In some cases, code may be written in such a way that form elements themselves (and not a fully themed form) need to be returned to a calling function. This is the case with many Drupal core hooks. in this case, simply return the constructed $form array, and let the calling code handle the form rendering..."

...as both circumstances are coming up in this upgrade effort. Any insight into this would be most appreciated!

Comments

eaton’s picture

My question is: when is it okay to leave the comma off the end and why?

Both are acceptable, though the first (the recommended style with the additional comma) is 'standard.' The two wtyles are treated as equivalent by PHP, but when copying and pasting form defintions, adding and changing individual elements etc, it's much easier to avoid *missing* including a comma and causing sytax errors if ALL the lines in the form definition have them.

Pure convenience. PHP treats the two styles identically. :)

--
Eaton's blog | VotingAPI discussion

--
Eaton — Partner at Autogram