Calling User::save() on a user in a custom script breaks the realname settings for that user.

Created on 30 May 2019, over 5 years ago
Updated 1 February 2023, almost 2 years ago

I have a script I'm writing that grabs some user data out of an ldap directory and puts it into the appropriate drupal user. I have run into something odd. It appears that calling \Drupal\user\Entity\User::save() on a user in a custom script breaks the realname settings for that user.

Before running the script, realname works just fine. The value of field_display_name is used in place of the users username. But after running it, the users username shows up instead of the value of field_display_name. If I manually edit the user, changing nothing, but saving anyway, via the user edit page, realname starts working again.

In case that is clear as mud:

* User has a login of "KingKong"
* User has a display name of "John Smith"
* Before running the custom script, /user/john-smith displays "John Smith" as the users name.
* After running the custom script, /user/john-smith displays "KingKong" as the users name.
* After saving user//edit, /user/john-smith displays "John Smith" as the users name, again.

My script is in the same directory as load.environment.php. I am running it using Drush 9's php:script command.

I tested stripping everything except a call to User->save(), and it displayed the same behavior.


use  \Drupal\user\Entity\User;

require 'load.environment.php';
$ids = \Drupal::entityQuery('user')
  ->condition('status', 1)
  ->execute();
$users = User::loadMultiple($ids);
foreach ($users as $subUser) {
  $subUser->save();
}

I tried calling realname_update($subUser) after save(), but it only returned an empty string.

I'm running Drupal 8.7.1, realname 8.x-1.0-rc2.

Do you have any suggestions on how I can get my script to reset the realname settings?

Thanks!

πŸ› Bug report
Status

Active

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States jerrac

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡©πŸ‡ͺGermany Elin Yordanov

    I'm having the same problem on cron or updates. I have a hook_cron implementation which daily updates user's with some specific criteria. Those users' realnames are broken. The same problem occurs if I write a hook_update_N implementation to a module and run the updates via drush.

Production build 0.71.5 2024