I am trying to programmatically create a user in Drupal 8 using the following code.
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$user = \Drupal\user\Entity\User::create();
$user->setPassword($form_state->getValue('pass'));
$user->enforceIsNew();
$user->setEmail('[email protected]');
$user->setUsername('test_test');
$user->set("init", '[email protected]');
$user->set("langcode", $language);
$user->set("preferred_langcode", $language);
$user->set("preferred_admin_langcode", $language);
$user->activate();
$user->save();
When I execute it, I get the following exceptions.
Drupal\Core\Entity\EntityStorageException: Field type is unknown. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 783 of /home/printhouseheroes/public_html/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
InvalidArgumentException: Field type is unknown. in Drupal\Core\Entity\ContentEntityBase->getTranslatedField() (line 586 of /home/printhouseheroes/public_html/core/lib/Drupal/Core/Entity/ContentEntityBase.php).
How can I avoid getting those exceptions?