0

I'm using the email registration module so that users can log in using their email address. The users are all from the same company, so I want to know if there's a way to have the '@company.com' part of the email as fixed text, like those grayed-out input fields. Someone knows if it's possible?

EDIT: I'm hoping to have something like this.

5
  • wouldn't that just be user names... as in default behaviour for Drupal?
    – Geoff
    Commented Jul 29, 2014 at 2:52
  • I'm not sure what you mean by default behavior... what I want is for the users not to type the domain part of the email (@company.com), but to have it by default. I've added a fiddle to explain it better.
    – brunn
    Commented Jul 29, 2014 at 4:10
  • well, not using the entire email address, is just like entering a user name - the default behaviour, if they use their email address (bob of [email protected]) as their user name, since as you stated it will ALWAYS be the same. I fail to see the reasoning behind this...
    – Geoff
    Commented Jul 29, 2014 at 4:22
  • Ah, now I get what you mean. Problem is our client wants employees to register and log in using their company email address.
    – brunn
    Commented Jul 29, 2014 at 4:26
  • so basically what you want is for them to register ([email protected]) as their "user name" but only have to key in the "bob" at login, to save time and effort on their part? If so, yes it can be done, look into hook_form_alter() - you would alter the submit function to append the @domain.com to the user name before being submitted. Actually showing it can be done through theme'ing.
    – Geoff
    Commented Jul 29, 2014 at 5:21

2 Answers 2

0

Look at email_registration_user_login_validate(), if there is no user with such email, drupal searchs user with such name. This module as I understood creates name from email by email_registration_cleanup_username(). So you can add own validate callback as the module do and pass name value throw this function. Or better to add domain suffix. Something like this:

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * FORM_ID need to set to 'user_login' or 'user_login_block' or some else you have.
 */
function mymodule_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
  array_unshift($form['name']['#element_validate'], 'mymodule_user_login_name_validate');
  $form['name']['#suffix'] = '<span class="name-email-domain">@domain.com</span>';
}
function mymodule_user_login_name_validate($form, &$form_state) {
  $values = &$form_state['values'];
  if (!isset($values['name'])) {
    return;
  }
  //$values['name'] = email_registration_cleanup_username($values['name']);
  $values['name'] .= '@domain.com';
}
7
  • I tried using this code but I get a blank page. I'm changing 'mymodule' to 'email_registration' and 'FORM_ID' to 'user_login', but it's not working. I don't usually work with hooks, so I don't know what could be wrong...
    – brunn
    Commented Jul 30, 2014 at 2:29
  • Did you clear caches after this functions were inserted? If yes then try mymodule_form_alter(&$form, &$form_state, $form_id) and dump $form_id in it.
    – FriOne
    Commented Jul 30, 2014 at 4:45
  • Thank you, that did it! Except I get a warning: array_unshift() expects parameter 1 to be array, null given in email_registration_form_alter()
    – brunn
    Commented Jul 30, 2014 at 5:12
  • @brunn add $form['name']['#element_validate'] = array(); before array_unshift().
    – FriOne
    Commented Jul 30, 2014 at 5:36
  • @brunn It worked incorrect way(Yours previous comment).
    – FriOne
    Commented Jul 30, 2014 at 5:38
0

You can achieve this using the hook_form_alter API. Alter the email field with something like $form['field_email']['#default_value'] = '' . '@company.com';

That will always append the @company.com to each email entered.

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.