Drupal form api – how it should have been

I find myself spending way too much time on theming form elements and links, and I’m constantly removing Drupals own styling – div and span tags that shouldn’t be there in the first place.For instance, have a look at theme_form_element, now why on earth do we need the <div class=”form-item”> when I have the #suffix and #prefix and although the #prefix is described as “Text or markup to include before the form element” what it should say was “Text or markup to include inside the wrapper and before the form element”.

I know I can just override the theme_form_element but it shouldn’t be necessary, in stead the theme_form_element should look like this:

function theme_form_element($element, $value) {
 // This is also used in the installer, pre-database setup.
 $t = get_t();

 $required = !empty($element['#required']) ? '<span title="'. $t('This field is required.') .'">*</span>' : '';

 if (!empty($element['#title'])) {
  $title = $element['#title'];
   if (!empty($element['#id'])) {
    $output .= ' <label for="'. $element['#id'] .'">'. $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
   }
   else {
    $output .= ' <label>'. $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
   }
 }

 $output .= " $value\n";

 if (!empty($element['#description'])) {
  $output .= ' <div>'. $element['#description'] ."</div>\n";
 }

 return $output;
}

- now I can actually wrap my form element using #prefix and #suffix.

Please Drupal Core Team: Layout doesn’t go into the functions!

By taking the layout out of the core functions, you can theoretically apply any theme to your drupal site instead of having to either build you markup based on Drupal, or override a whole bunch of core functions.

About these ads

One response to “Drupal form api – how it should have been

  1. Im newbie in drupal, and this is very interest topic, nice information for me.thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: