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!