简体   繁体   中英

Add classes to Drupal 7 register form

So I have been using drupal 7 to develop a website. I wanted to override the default login and register forms. I was able to successfully add custom classes to the login and password forms. But the problem started when I wanted to add those same classes to the register form.

I used this hook function to try and add the classes

function MyTheme_form_user_register_form_alter(&$form, &$formState){
    // set classes;
    $form["#attributes"]["class"] = "form-element";
    $form['field_first_name']['und'][0]['value']['#attributes']['class'][] = 'form-element__control';
    $form['field_last_name']['und'][0]['value']['#attributes']['class'][] = 'form-element__control';
    $form['field_developer_organization']['und'][0]['value']['#attributes']['class'][] = 'form-element__control';
    $form['account']['mail']['#attributes']['class'][] = 'form-element__control';
    $form['account']['pass']['pass1']['#attributes']['class'][] = 'form-element__control';
    $form['account']['pass']['pass2']['#attributes']['class'][] = 'form-element__control';
    $form['account']['captcha_response']['#attributes']['class'][] = 'form-element__control';

    // remove descriptions;
    $form['field_first_name']['und'][0]['value']['#description'] = t('');
    $form['field_last_name']['und'][0]['value']['#description'] = t('');
    $form['field_developer_organization']['und'][0]['value']['#description'] = t('');
    $form['account']['mail']['#description'] = t('');
    $form['account']['pass']['pass1']['#description'] = t('');
    $form['account']['pass']['pass2']['#description'] = t('');
    $form['account']['captcha_response']['#description'] = t('');
}

For the firstname, lastname, developer organisation, email, the classes are added successfully, but for the password fields and the captcha fields the classes are not added

What could be the issue?

In your code snippet, you're altering the user_register_form() form. And it's simply a wrapper for the core user account form fields, custom profile fields and other essentials.

Try altering user_account_form() form the same way, to modify class names for username and password fields.

And in each form's documentation you can see what fields can be altered directly, and what other forms make calls to them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM