drupal 8 login security code example

Example 1: drupal 8 user_load

function user_load($uid, $reset = FALSE) {
  if ($reset) {
    \Drupal::entityManager()
      ->getStorage('user')
      ->resetCache(array(
      $uid,
    ));
  }
  return User::load($uid);
}

Example 2: drupal create user

$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$user = \Drupal\user\Entity\User::create();

//Mandatory settings
$user->setPassword('test');
$user->enforceIsNew();
$user->setEmail('[email protected]');
$user->setUsername('test'); //This username must be unique and accept only a-Z,0-9, - _ @ .

//Optional settings
$user->set("init", 'email');
$user->set("langcode", $language);
$user->set("preferred_langcode", $language);
$user->set("preferred_admin_langcode", $language);
//$user->set("setting_name", 'setting_value');
$user->activate();

//Save user
$user->save();

Tags:

Misc Example