So user_save() claims to return a fully-loaded user object but in some cases it doesn't seem to. I'm filing this as a bug report, but if my example is flawed it might just be a support request.

Suppose, for example, we are creating a new user. Mimicking user_external_login_register(), we call something like

$edit = array(
  'name' => user_password(),
  'pass' => user_password(),
  'init' => user_password(),
  'status' => 1,
);
$account = user_save(drupal_anonymous_user(), $edit);
print_r($account);

Running this with drush scr returns something like:

stdClass Object
(
    [name] => ew6A4Jy2jR
    [pass] => $S$C54k3AiWEzHzrCe8XrfIhKd0MaI6baCNtFH5LknMHjSC66so1AO9
    [init] => hELVnoibpg
    [status] => 1
    [uid] => 188
    [created] => 1307060932
    [mail] => 
    [theme] => 
    [signature] => 
    [access] => 0
    [login] => 0
    [language] => 
    [picture] => 0
    [roles] => Array
        (
            [2] => authenticated user
        )
)

This is not a fully-loaded $user object. For example, it's missing the timezone field (as reported in #1176904: Initial login generates a timezone warning). Am I doing something wrong here? Is user_save flawed? Or is the documentation wrong?

Comments

justintime’s picture

Subscribe. Having the same problem as indicated in #1176904: Initial login generates a timezone warning.

geerlingguy’s picture

Version: 7.x-dev » 8.x-dev

This same problem causes things like #935592: User picture is deleted after calls to user_save(), since sometimes a $user object contains just the picture file ID (an integer), and other times, the $user object contains the full picture file object (an object).

It would be nice if we could depend on the user object being consistent :)

And I'm guessing this would still be a problem in 8.x... and should be solved there first.

longwave’s picture

user_save() no longer exists in D8, instead you must call ->save() on a User entity, so I am not sure this is an issue there.

geerlingguy’s picture

It would be good to confirm before moving back to 7.x, though—I haven't had time to look through all the changes to user-related functionality in D8, and a lot has changed... but a lot has remained the same :)

lokapujya’s picture

Just verified that in D8, after calling save() on the user entity, that the timezone field gets set. We have UserSaveTest; It only checks to see if the user can be loaded by name. Do we need to expand the test to prove that it's "fully loaded"? The webTestBase drupalCreateUser() function claims to return a fully loaded user object and it's just doing save() on the entity.

ws.agency’s picture

Status: Active » Closed (won't fix)

user_save is removed from D8, therefore closing this ticket.

lokapujya’s picture

Status: Closed (won't fix) » Active

But we can still save() the user object. I think we still need to answer #5.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dpi’s picture

user_save() no longer exists. everything goes through the same entity storage controller.

Closing as outdated.

dpi’s picture

Status: Active » Closed (won't fix)