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';

dsm($form);

$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!

Comments

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

eg:

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

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.

Regards,
Mitch

cosminadrianpopescu’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 tags so it prints properly!

- Amanda

You know that you are stupid, don't you?-

Cosmin Popescu

welly’s picture

Do you need to be so rude? I'll be reporting this comment.

WorldFallz’s picture

You know that you are stupid

This is unacceptable on drupal.org. Your comments repeatedly demonstrate lack of courtesy and respect. If you can't use common courtesy, your account will be blocked. Please see http://drupal.org/dcoc for more info.

for reference: #2142543: Spam Report.

_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.

Jaypan’s picture

You know that you are stupid, don't you?-

I personally will be making note of your name, and will not help any support request you make in the future if you are going to have that attitude. Not cool at all.

SangersDrupalDude’s picture

Actually alleged stupid people and idiotic ill-mannered rude people, the most correct way of doing this:

  if(!isset($form]['#attributes']['class'])){
    $form['#attributes'] = array('class' => 'classname');
  }
  else{
    $form['class'][] = 'classname';
  }

cosminadrianpopescu your code would replace any classnames that already exist in the code, (yes I did use code tags). Would you please not post rude comments and bad code simultaneously! We are here to help eachother, try and be nice.

Senior Drupal Web Developer
Wellcome Trust Sanger Institute

bkosborne’s picture

More like this:

  if (!isset($form['#attributes']['class'])) {
    $form['#attributes'] = array('class' => array('form-inline'));
  }
  else {
    $form['#attributes']['class'][] = 'form-inline';
  }

The 'class' property is an array of classes.

Max_Headroom’s picture

Thanks for the tip. For multiple classes, I did this:

<?php
 $classes
= array('col-xs-12', 'col-sm-12', 'col-md-12', 'col-lg-12');

      if (!isset(
$form['#attributes']['class'])) {
       
$form['#attributes'] = array('class' => $classes);
      }
      else {
       
$form['#attributes']['class'] = array_merge($form['#attributes']['class'], $classes);
      }
?>

Quentin Campbell