I'm trying to add a class attribute to the user-login-block form. I've used the following code in template.php:

function theme_user_login_block($form) {

$form['#attributes']['class'] = 'test';


$output = drupal_render($form);

return $output;


The output of the DSM function indicates that the class has been added to the attribute property of the form array but the class does not appear in the form element html. Here is the beginning of the form html:

 <form action="/mycommunity/node?destination=node"  accept-charset="UTF-8" method="post" id="user-login-form">

i would have expected the class attribute to be included in this part of the form html. Am i missing something?

many thanks for any help

update: code tags included - thanks Amanda!


mndonx’s picture

Hi there -
I believe the correct way to write it is:

$form['#attributes'] = array('class' => 'classname');

Also, don't forget to wrap any code you post here in <code> tags so it prints properly!

- Amanda

OliverMcc’s picture

thanks Amanda,

i tried that but got the same result. Maybe the
element can't take a class??

mndonx’s picture

Hmm, I think you could be right.

Would you consider wrapping your form in a div instead?

  $form['#prefix'] = '<div class="myclass">';
  $form['#suffix'] = '</div>';

This doesn't actually wrap around <form></form>, but rather all the content inside of the form tags.... which also makes me think that that form tag doesn't get messed with, only the contents. But I'm definitely not an expert on FAPI!

mpaler’s picture

using hook_form_alter


function your_module_form_alter(&$form, $form_state, $form_id) {
  switch($form_id) {
    case 'id of target form':
      $form['#id'] = 'new_id_of_form';

this will be rendered as

<form blah blah id="new_id_of_form">

See page 232 of the Pro Drupal Development book.

christowm’s picture

Hi folks,

I know this is an old post, but for those that are running into the same problem. The fix is to append the value since 'class' is a nested array. So the correct code to add a value would be:

$form['#attributes']['class'][] = 'test';

Note the empty bracket after the class variable. That will do the trick.


rcodina’s picture


dany525’s picture

hi how are you i wanna know how do from edit the divs on a form of drupal?