0

I have an address field on my User entities. I want to programmatically create users and set their address field values, I currently have this:

 $values = [
  'mail' => 'chris+7asd7fadf7afdafasdf@example.com',
  'name' => 'chris+7asd7fadf7afdafasdf@example',
  'first_name' => 'First',
  'last_name' => 'Last',
  'roles' => [],
  'pass' => user_password(),
  'status' => 1,
];
$user = User::create($values);
$user->field_contact_information->country = 'US';
$user->field_contact_information->given_name = $values['first_name'];
$user->field_contact_information->family_name = $values['last_name'];
$user->save();

But it's not saving the values to field_contact_information. Elsewhere in the app, I do access these values with $user->field_contact_information->given_name.

2

That was simple - it should be $user->field_contact_information->country_code = 'US';

1
  • 2
    You can always see the column names by looking at the field class (e.g. AddressItem) and its property definitions
    – Berdir
    Jul 16 '17 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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