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?

MRPRAVIN’s picture

'#attributes' => array('class' => array('your_class_one', 'your_class_two')),

sfelder’s picture

Just what I was looking for. Didn't know class needed to be its own array.

PatrickMichael’s picture


themename_preprocess_form(&$variables) {
  $variables['element']['#attributes']['class'][] = 'my-class';

ksm($variables['element']['#attributes']['class']) shows the class has been added but not to the form output

Any idea how to do this in D8?

chrisrockwell’s picture

It might work for you, but $variables['attributes'] is a Drupal\Core\Template\Attribute which has an addClass method (as well as setAttribute). Using these methods I was able to successfully set classes/attributes:

if ($variables['element']['#type'] == 'radio' && strpos($variables['element']['#id'], 'edit-purchased-entity-0-variation') !== FALSE) {
    $variation = \Drupal\commerce_product\Entity\ProductVariation::load($variables['element']['#return_value']);
    $variables['attributes']->setAttribute('data-js-pricing-length', $variation->getAttributeValue('attribute_length')->label());