I have created a webform which when submitted will create a new user for my Drupal 8 site.
The user must enter his username, email, and some additional data.
I am using below code to create & validate a user:
namespace Drupal\my_module\Plugin\WebformHandler;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\node\Entity\Node;
use Drupal\user\Entity\User;
// use Drupal\taxonomy\Entity\Term;
use Drupal\webform\WebformInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\webformSubmissionInterface;
use Drupal\webform\Entity\WebformSubmission;
/**
* Create New User Account when a webform submission is submitted.
*
* @WebformHandler(
* id = "Create New User Account",
* label = @Translation("Create New User Account"),
* category = @Translation("Entity Create"),
* description = @Translation("Create New User Account when a webform submission is submitted."),
* cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
* results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
* submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
* )
*/
class CreateNewAccountHandler extends WebformHandlerBase {
/**
* {@inheritdoc}
*/
// Function to be fired while submitting the Webform.
public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
// Build Drupal DateTime & set correct format for your query.
$current_time = \Drupal::time()->getCurrentTime();
// Get an array of the values from the submission.
$values = $webform_submission->getData();
// Get 'Username' Value.
$username = $values['username'];
// Get 'Password' Value.
$password = $values['password'];
// Get 'Full Name' Value.
$FullName = $values['full_name'];
// Get 'Phone Number' Value.
$PhoneNumber = $values['phone_number'];
// Get 'Email' Value.
$email = $values['email'];
// Create user object.
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$new_user = \Drupal\user\Entity\User::create();
//Mandatory fields.
$new_user->setPassword($password);
$new_user->setEmail($email);
$new_user->setUsername($username); // Alphanumeric (abc, 123), space ( ), period (.), hyphen (-), apostrophe ('), underscore ( _ ), and the @ sign are only allowed.
$new_user->set("langcode", $language);
$new_user->set("preferred_langcode", $language);
$new_user->set("preferred_admin_langcode", $language);
$new_user->enforceIsNew(); // Set this to FALSE if you want to edit (resave) an existing user object.
//Optional Fields
$new_user->set("init", 'email');
// Custom Fields
$new_user->set("field_full_name", $FullName);
$new_user->set("field_ph_nbr", $PhoneNumber);
// Validate the user for possible errors.
$violations = $new_user->validate();
if (count($violations) > 0) {
$property = $violations[0]->getPropertyPath();
$msg = $violations[0]->getMessage();
// Show a custom message to user.
\Drupal::messenger()->addError(t("$msg"));
return FALSE;
}
// Activate the user Object (Status = Active).
$new_user->activate();
//Save user
$new_user->save();
// Get the new user ID.
$new_user_id = $new_user->id();
// Log in the user.
user_login_finalize($new_user);
However, every time I try to create a new user using the webform, I got the below error:
This value is not a valid email address.
I have tried with:
What is wrong with my code ?
$new_user = User::create([ 'name' => $user_name, 'mail' => $user_mail, ]);
part.$new_user->set("init", 'email');
setEmail