Change #value for #default_value on email_registration_form_user_register_form_alter()
This way we can use JS to alter the value of the input. instead of using hook_email_registration_name(). Which has the benefit of not needing an update query to the database just after inserting the user.
--- a/email_registration.module
+++ b/email_registration.module
@@ -120,7 +120,7 @@ function email_registration_cleanup_username($name, $uid = NULL) {
*/
function email_registration_form_user_register_form_alter(&$form, &$form_state) {
$form['account']['name']['#type'] = 'hidden';
- $form['account']['name']['#value'] = 'email_registration_' . user_password();
+ $form['account']['name']['#default_value'] = 'email_registration_' . user_password();
$form['account']['mail']['#title'] = t('E-mail');
}| Comment | File | Size | Author |
|---|---|---|---|
| #3 | email_registration_default_value.patch | 613 bytes | jackbravo |
Comments
Comment #1
gregglesThanks for the idea. Can you attach this as a patch and show how it might change this to not needing the update query?
Comment #2
jackbravo commentedHi greggles
Here is the patch.
The problem is that right now the module uses hook_user_insert() to change the username. First it does this check:
So if you provide a different account name from the create user page (with jquery since the field is hidden) then you skip this hook, that further down the line does this inside that same hook_user_insert:
The problem is that you can't know the email of the new user before generating the create user form. So you have this code that I'm proposing to change:
That provides a __#value__ for the account->name. I'm suggesting changing that to __#default_value__. And I have this jQuery code in my theme:
I'm not sure if we could add this jQuery code to a behaviour inside the module instead. But at least providing the opportunity to use this solution by using __#default_value__ sounds like a good alternative right? =).
Comment #3
jackbravo commentedUps, forgot to add the patch
Comment #4
gregglesThe risk with a #default_value on a 'hidden' field is exactly the feature you are using: it can be edited by the client.
So, if someone knows how to edit the html they could use any name they want. I'm not sure that's a big problem or not, but it does change the behavior of the module a bit.
It's not clear to me how this is helpful. Does it save X milliseconds per user insert or something?
Comment #5
jackbravo commentedIt doesn't sound like a big issue to me. Specially considering that after login, the user is allowed to change his "Display name" (account name).
The issue I have is when integrating with https://www.drupal.org/project/ldap. The LDAP module creates a user in my ldap server before the insert. And then creates a second one when the update runs.
Comment #6
jackbravo commentedSo, what I'm saying is that the risk is already there, since the user already has permission to modify his username. So this saves a few milliseconds by not calling the update query, which seems to also call other hooks as well. For example in my case, the update query seems to call an update hook for the ldap module that creates two ldap users with two different account names, one for the email_registration_XXXXX user and another for the updated account name user.
Comment #7
jackbravo commentedComment #8
gregglesThe risk is only there on some sites, though, since that permission is not on all sites.
I'll think on this further. The risk seems rather small, especially since the only downside is that someone has created their own username...on a site with email_registration the username usually isn't displayed very much.
Comment #9
agerson commented+1 for need this module to work in conjunction with LDAP.
Comment #10
agerson commentedjackbravo, with your patch I was not able to have existing LDAP users log in with their email.
Comment #11
jackbravo commented@agerson are you also using the email_registration module? What issue are you seeing?
Comment #12
agerson commentedI am. "Local" Drupal users can log in with their email. LDAP synced users can not.
Comment #13
jackbravo commentedThat sounds strange..... email registration module should allow that. Anyway, the way we do it is we set the LDAP module configuration so that synced users have their email used as account name. That's how we do it and it is working on two sites.
Comment #14
agerson commentedWhere is that setting? And then they can not login with just their username right?
Comment #15
jackbravo commentedTheir email becomes their username.
Comment #16
bluegeek9 commentedDrupal 7 is End of Life. We recommend upgrading to a supported version of Drupal.