The way that form item attributes are currently handled is not ideal.
<?php
function form_textfield($title, $name, $value, $size, $maxlength, $description = NULL, $attributes = NULL, $required = FALSE) {
$size = $size ? ' size="'. $size .'"' : '';
return theme('form_element', $title, '<input type="text" maxlength="'. $maxlength .'" class="'. _form_get_class('form-text', $required, _form_get_error($name)) .'" name="edit['. $name .']" id="edit-'. $name .'"'. $size .' value="'. check_form($value) .'"'. drupal_attributes($attributes) .' />', $description, 'edit-'. $name, $required, _form_get_error($name));
}
?>
The name and id attributes are required so that the input and label elements will function as they should. Using the $name value for the id attribute is problematic though.
1. $name is sometimes overloaded with multiple values (e.g. "foo][1][2") and the brackets are closed for the name attribute but not for the class attribute.
2. Using a different id than $name is not supported.
3. Adding additional classes is not supported. If a "class" attribute were added in the $attributes variable it would show up as another attribute instead of being appended to existing class attribute.
The following is an attempt to provide more flexibility for the id and class attributes by allowing them to be specified in the $attributes argument.
<?php
function form_textfield($title, $name, $value, $size, $maxlength, $description = NULL, $attributes = NULL, $required = FALSE) {
$size = $size ? ' size="'. $size .'"' : '';
$class = _form_get_class('form-text', $required, _form_get_error($name));
if ($attributes) {
$id = (isset($attributes['id']) ? 'id="'.$attributes['id'].'"' : 'id="edit-'.$name.'"';
$class .= (isset($attributes['class']) ? " $attributes['class']" : '');
$attributes = array_diff_key($attributes, array('id' => '', 'class' => ''));
}
return theme('form_element', $title, '<input type="text" maxlength="'. $maxlength .'" class="'. $class .'" name="edit['. $name .']" id="edit-'. $name .'"'. $size .' value="'. check_form($value) .'"'. drupal_attributes($attributes) .' />', $description, 'edit-'. $name, $required, _form_get_error($name));
}
?>
Of course handling attributes in multiple ways may be confusing so it may be more desirable to support them as individual arguments.
Note that I haven't tested the above code but it isn't a patch so I'm comfortable with that. I am more interested in comments for now.
Comments
Comment #1
tangent CreditAttribution: tangent commentedHere's a cleaner version.
Comment #2
tangent CreditAttribution: tangent commentedHere's a version that adds more arguments.
Comment #3
tangent CreditAttribution: tangent commentedI believe the new Forms api resolves this issue. Feel free to close this if so.
Comment #4
tangent CreditAttribution: tangent commentedWith Forms API, unsupported attributes can be specified with the
#attributes
property.Comment #5
(not verified) CreditAttribution: commented