1

I am having problems getting a custom module to recognize the user login block.

Ultimately I need my user login block to only show the username and password fields, without any labels. Instead of labels I need to prefill the form fields with the words 'username' and 'password' and have this text disappear onfocus. This drupal answers question demonstrates how to do this with the search block and a custom module. I have been able get this to work without any problems.

I have tried to do a similar thing with the user login block.

First of all i used this to get the form id:

function custom_prefill_login_form_alter(&$form, $form_state, $form_id) {

 // uncomment to get form id
 drupal_set_message("Form ID is : " . $form_id);
 }

This returns Form ID is : user_login_block for the login block page and it returns Form ID is : search_block_form for the search block

When on a page with the search block enabled I can use this:

if ($form['#id'] == 'search-block-form') {  
    dpm('search-block-form showing');
}

but when on the user login block page this doesn't work. Neither of these show anything in the message area:

 if ($form['#id'] !== 'user-login-block') {  
     dpm('user-login-block showing');
 }

 if ($form['#id'] !== 'user_login_block') {  
    dpm('user_login_block showing');
}

I am very confused as to why this isn't working. I would appreciate being shown how to write an if statement that targets the user login block.

My entire function is:

function custom_prefill_login_form_alter(&$form, $form_state, $form_id) {

    // uncomment to get form id
    drupal_set_message("Form ID is : " . $form_id);

    if ($form['#id'] !== 'user-login-block') {  
          dpm('user-login-block showing');
      }

    if ($form['#id'] !== 'user_login_block') {  
          dpm('user_login_block showing');
      }

    if ($form['#id'] == 'search-block-form') {  
          dpm('search-block-form showing');
          $search_text = 'Your search text here';
          $form['search_block_form']['#default_value'] = $search_text;
           $form['search_block_form']['#attributes'] = array(
          'onfocus' => "if (this.value == '$search_text') {this.value = '';}",
          'onblur' => "if (this.value == '') {this.value = '$search_text';}");
      }

  }

2 Answers 2

1

To target a specific form you can use hook_form_FORM_ID_alter(), e.g.:

function MYMODULE_form_user_login_block_alter(&$form, &$form_state, $form_id) {
  $form['foo']['#default_value'] = 'bar';
}

If you'd rather do it all in one alter hook though, use the provided $form_id parameter instead of $form['#id']...it's guaranteed to be the right one.

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_login_block') {
    $form['foo']['#default_value'] = 'bar';
  }
}

As a side note, it's probably not working at the moment because you're negating the condition:

if ($form['#id'] !== 'user-login-block') {  

should be

if ($form['#id'] === 'user-login-block') {  
1
  • 1
    Also realize that if you are going to use your onfocus/onblur method above, it probably won't work with the password field, eg, it will show up as black dots or however your browser obscures the password as it's entered instead of your actual default password text.
    – Jimajamma
    Mar 12, 2014 at 18:17
1

Clive's answer is correct, and Jimajamma's comment is valid

So working custom module code for this is:

function custom_prefill_login_form_user_login_block_alter(&$form, &$form_state, $form_id) {
 print_r("working");
 $form['name']['#default_value'] = 'Username';
}

Where the custom module is named custom_prefill_login

But as Jimajamma explained this doesn't work with the password field. I'm investigated a little and we can set the placeholder text in drupal too. So here is the complete solution:

function custom_prefill_login_form_user_login_block_alter(&$form, &$form_state, $form_id) {
 $form['name']['#attributes']['placeholder'] = t('Username');
 $form['pass']['#attributes']['placeholder'] = t('Password');
}
2
  • What about $form['pass']['#attributes']['placeholder'] = 'Some placeholder'? Placeholders work on password inputs, and are nicer than pre-filling the value IMO
    – Clive
    Mar 13, 2014 at 10:29
  • Yep Clive - I reached that conclusion too before reading your comment. ps cheers for your help on this :) Mar 13, 2014 at 10:39

Your Answer

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

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