0

I have a small problem on D7, I need to alter the login form inside block (add class to inputs, placeholder and submit button text, custom pass reset and login links) im using hook_form_FORM_ID_alter. I don't need to use custom module.

block id = block-user-login, form id = user_login_block

My theme.php code

function themename_form_user_login_block_alter(&$form, &$form_state) {

    $form['name']['#attributes']['placeholder'] = t( 'Username' );
    $form['pass']['#attributes']['placeholder'] = t( 'Password' );

   // custom register link
    $form['actions']['signup'] = array(
        '#markup' => l(t('Register'), 'user/register', array(
            'attributes' => array(
                'class' => 'top-user-block-link register',
                'title' => t('Create a new user account.')
            )
        )),
    );

    // custom reset password link
    $form['actions']['request_password'] = array(
        '#markup' => l(t('Forgot my password'), 'user/password', array(
            'attributes' => array(
                'class' => 'top-user-block-link new-password',
                'title' => t('Request new password.'),
            )
        )),
    );
}

Links for password reset and login links renders fine, but i can't make the placeholders or class add to work.

i've tried to use

function theme_form_alter(&$form, &$form_state, $form_id){
  if ($form_id === 'user_login_block') {
$form['name']['#attributes']['placeholder'] = t( 'Username' );
    $form['pass']['#attributes']['placeholder'] = t( 'Password' );
  }
}

but i works only on /login page.

2 Answers 2

1
$form['name']['#attributes']['placeholder'] = t( 'Username' );
$form['pass']['#attributes']['placeholder'] = t( 'Password' );

Both the code are working. If you can't see your changes or placeholder.

Please try with var_dump($form). You will find $form_id and all other fields of the form. just check your debug (array) ordering and make sure that order is same as in your form_alter() function.

3
  • var_dump($form) do the job, it was some custom login form generated form module. Simply i was calling bad id. Apr 14, 2017 at 9:08
  • Now, Working? Or need help ? Apr 14, 2017 at 9:12
  • Yes, work perfect ! :) thx ! Apr 14, 2017 at 9:15
1

If I recall correctly, you need $form['account']['name']['#attributes'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.