Hoppa till huvudinnehåll

How to create a user account programmatically in Drupal 8

Here you can read how to create a user programmatically in Drupal 8

Creating a user account is one of the most important task in a web page.

In the next example we are creating a user account with the values given in a form.

public function submitForm(array &$form, FormStateInterface $form_state) {

  $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  $user = \Drupal\user\Entity\User::create();
  $user->setUsername($form_state->getValue('name'));//This username must be unique and accept only a-Z,0-9, - _ @ .
  $user->set("init", 'mail');
  $user->set("langcode", $language);
  $user->set("preferred_langcode", $language);
  $user->set("preferred_admin_langcode", $language);
  //Save user account

  // No email verification required; log in user immediately.
  _user_mail_notify('register_no_approval_required', $user);
  drupal_set_message($this->t('Registration successful. You are now logged in.'));

Vi hjälper dig nå resultat. Kontakta oss Ring direkt på 08-20 90 04.

Cache in Drupal 8: How it worked, and what we wanted to be solved in D8

If you just want your content to be cached before Drupal 8, there were almost no problems, just turn on caching for anonymou…

Cache in Drupal 8: Introduction

In a series of blog posts we will go through how caching works in Drupal 8, to try to demystify the caching layer in Drupal …