I have tried the following code but nothing seems to be working. I have removed the other field just to make focus on status field.

$user = \Drupal\user\Entity\User::create();
$user->set("status", 0);
// Save user
$saved = $user->save();

Another code is

$user = \Drupal\user\Entity\User::create();
$user->status= 0;
// Save user
$saved = $user->save();

But none of the above code is working. Please guide how to set the user status. Here is my full code

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

    $user_data = db_query("SELECT * from eberhard_users ORDER BY uid ASC limit 0,1");

    foreach ($user_data as $delta => $details) {
        // The Basics
        $password = user_password(8);
        $user->uid = $details->uid;
        $user->created = $details->created;
        $user->changed = $details->login;
        $user->access = $details->access;

        $user->enforceIsNew();  // Set this to FALSE if you want to edit (resave) an existing user object

        // Optional settings  <-- Thanks to http://drupal8.ovh/ for these suggestions!
        $user->set("init", $details->mail);
        $user->set("langcode", $lang);
        $user->set("preferred_langcode", $lang);
        $user->set("preferred_admin_langcode", $lang);
        $user->set("timezone", 'Pacific/Wallis');
        $user->set("status", FALSE);
        // Save user
        $saved = $user->save();die;
  • You haven't told us what you're trying to do, and your code doesn't make sense, as you're setting the status to 0, then setting it to 1 (activating).
    – Jaypan
    Apr 20, 2017 at 11:12
  • I am importing users, some of which has active and some has inactive status. SO I want to make sure that proper status will be saved. Does that make sense to you? Apr 20, 2017 at 11:26
  • Yes. Then you need to remove the code setting the status to 0 (zero). You will also need to set a username, and likely a password as well, though I'm not entirely certain about the password.
    – Jaypan
    Apr 20, 2017 at 11:28
  • added full code. Please see. Apr 20, 2017 at 11:28
  • Jaypan, Any suggestions how can I set user status? Active for some users and Blocked for some? Apr 20, 2017 at 11:50

1 Answer 1


There are a number of issues with your code. First, you are setting status as false, then activating. You don't need to set the status as false first. Just create the user, and set activate() on it.

Next, you are trying to set properties directly, like the created. You cannot do that in Drupal 8. You need to use the methods on the user object. So you would use set(). Other properties have methods for setting the property, such as setEmail(). You can view all the methods on the User object here: https://api.drupal.org/api/drupal/core%21modules%21user%21src%21Entity%21User.php/class/User/8.3.x

One other issue is that you are setting the UID. I don't believe you can do this, as the user ID is determined by the auto-increment value in the database, and trying to manually set it, if you can get it to work, will only cause you issues.

  • There are a lot of issues in the code above. I agree with Jaypan. Maybe the better question would be what are you trying to achieve with the code?
    – absoludo
    Apr 20, 2017 at 12:10
  • Thanks, Your comment helped me to rectify my code. Now I see that 'activate' method is for setting active status and 'block' is for setting blocked status. Why I am using the old user id is because there are orders from this users so I don't want to mess those when I will import orders. Apr 20, 2017 at 12:11
  • In that case, you're fighting a really difficult battle. You'd be better off using the Migrate API to migrate your content - this will associate users with their content.
    – Jaypan
    Apr 20, 2017 at 12:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.