I'm currently getting data from a userdata endpoint but the postal address comes back in a json, so i decided to json_decode it to split the data and then put into a database when a new user registers/logs in.


function openid_connect_create_user($sub, $userinfo, $client_name) {
  /** @var \Drupal\user\Entity\User $account */
  $address = $userinfo['address'];
  $decodeAddress = json_decode($address);

  $streetAddress2 = $decodeAddress->{"street_address"};
  $townCity2 = $decodeAddress->{"locality"};
  $region2 = $decodeAddress->{"region"};
  $postalCode2 = $decodeAddress->{"postal_code"};
  $country2 = $decodeAddress->{"country"};

  $streetAddress = (string)$streetAddress2;
  $townCity = (string)$townCity2;
  $region = (string)$region2;
  $postalCode = (string)$postalCode2;
  $country = (string)$country2;


  $account = User::create([
    'name' => $userinfo['email'],
    'pass' => user_password(),
    'mail' => $userinfo['email'],
    'init' => $userinfo['email'],
    'family_name' => $userinfo['family_name'],
    'given_name' => $userinfo['given_name'],
    'nickname' => $userinfo['nickname'],
    'gender' => $userinfo['gender'],
    'birthdate' => $userinfo['birthdate'],
    'phone_number' => $userinfo['phone_number'],
    'street_address' => $streetAddress,
    'town_city' => $townCity,
    'region' => $region,
    'postal_code' => $postalCode,
    'country' => $country,
    'status' => 1,
    'openid_connect_client' => $client_name,
    'openid_connect_sub' => $sub,

  return $account;

The logging shows the data being split correctly but user::create doesn't save the data correctly, any ideas?

The data that isn't getting saved is:

  • street_address
  • town_city
  • region
  • postal_code
  • country
  • 5
    Are those fields you added to the user entity with the field UI? If so they are probably prefixed with field_
    – Brian
    Dec 15, 2016 at 17:20
  • Try with only fields that comes in with default drupal installation if the used is saved correctly then in that case.There is something wrong with your code I can confirm here if you have used extra fields in user entity then you must use proper field names (machine_readable) in drupal. Dec 16, 2016 at 6:32


Browse other questions tagged or ask your own question.