Jump to content

Make user email field unique and if not unique prevent the EmailNewUser module firing


DrQuincy
 Share

Recommended Posts

I want to make it so that both user name and email are unique in template users. I've got this which issues a warning, which is better than nothing:

wire()->addHookAfter('Pages::saveReady', function(\ProcessWire\HookEvent $event) {
	
	$page = $event->arguments(0);
	
	// Don't run on newly created pages
	if ($page !== null && $page->created !== 0) {
	
		if ($page->template == 'user') {
				
			$pages = wire('pages')->find('template=user');
			
			foreach ($pages as $p) {
				
				// Don't compare to current page
				if ($p->id != $page->id) {
						
					if ($p->email == $page->email) {
						
						wire('session')->warning('**' . $p->email . '** is already being used by [' . $p->name . '](/processwire/access/users/edit/?id=' . $p->id  . ') — it is highly recommended each account has a unique email address', \ProcessWire\Notice::allowMarkdown);
						
					}
					
				}
				
			}
			
		}
		
	}
	
});

I am also using the EmailNewUser module. The reason I am issuing a warning as opposed to preventing the page being saved is regardless of what I do if you have the Send welcome email checkbox checked, EmailNewUser seems to always send the email out — and always to the duplicate email. I did try overriding the email to a random non-existent one so the email send would go nowhere — but EmailNewUser always emails the duplicate address, which is going to be confusing for the client.

Is there any way to adapt the above code to prevent EmailNewUser from running if the email is a duplicate? Or is there a way to make EmailNewUser used an updated random email instead? I'm not sure how it works but however EmailNewUser is set up it seems to always use the old value.

I hope that makes sense! Thanks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...