Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By OliverMcc on
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
Hi there - I believe the
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
thanks Amanda, i tried that
thanks Amanda,
i tried that but got the same result. Maybe the
element can't take a class??
Hmm, I think you could be
Hmm, I think you could be right.
Would you consider wrapping your form in a div instead?
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!You can do it using a custom module
using hook_form_alter
eg:
this will be rendered as
See page 232 of the Pro Drupal Development book.
Civicsolar
http://www.civicsolar.com
Hi folks, I know this is an
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
Works
Thanks!
hi
hi how are you i wanna know how do from edit the divs on a form of drupal?
Its Simple
'#attributes' => array('class' => array('your_class_one', 'your_class_two')),
Thanks
Just what I was looking for. Didn't know class needed to be its own array.
Trying to add class to form - Drupal 8.3x
using
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?
Just struggled with this, but on a form_element
It might work for you, but
$variables['attributes']
is aDrupal\Core\Template\Attribute
which has anaddClass
method (as well assetAttribute
). Using these methods I was able to successfully set classes/attributes:I hope this helps anyone.
I hope this helps anyone. Working with D 8.7
Adds class to div?
Hi. This appears to add the new class to a wrapping div around the form (the block), not the form itself. Is there a way to get the class added to the form element?
Check this out
Check this out guys https://drupal.stackexchange.com/questions/273540/add-class-to-form-tag-in-in-views-exposed-form-via-template-preprocess-views-exp
Maybe you aren't dealing with a views exposed form (as in my case) but this may still be applicable to you.
--
Heitor Althmann
Drupal Developer
Working D8 example
After some searching and dumping, this code helped me alter all fields on Drupal's default user edit page / form:
This article helped me with the password / repeat-password fields
https://www.lucius.digital/en
Drupal 8.8.2
On Drupal 8 i managed to add the class inside the <form> tag like this:
Add class array
add class array and it should work