0

I'm creating some taxonomy terms inside of an update hook and they have a coordinates field and an address field containing only zipcode and locality.

The Vocabulary is called cities, the address field is called field_cities_zip_city and the coordinates are called field_cities_coordinates.

//the exact data that fails
$city = [
  'zip' => 1000,
  'name' => 'Lausanne',
  'lat' => '47.3879577',
  'lng' => '8.044136',
];


$term = Term::create([
  'name' => $city['zip'].' '.$city['name'],
  'vid' => 'cities',
  'field_cities_zip_city' => [
    'postal_code' => $city['zip'],
    'locality' => $city['name'],
  ],
  'field_cities_coordinates' => [
    'lat' => $city['lat'],
    'lng' => $city['lng'],
  ],
]);
$term->save();

What happens is, the term gets created, the coordinates get filled in properly, but the address remains empty despite even being required fields.

Trying like this did not change anything:

//the exact data that fails
$city = [
  'zip' => 1000,
  'name' => 'Lausanne',
  'lat' => '47.3879577',
  'lng' => '8.044136',
];


$term = Term::create([
  'name' => $city['zip'].' '.$city['name'],
  'vid' => 'cities',
  'field_cities_zip_city' => [
    'postal_code' => $city['zip'],
    'locality' => $city['name'],
  ],
  'field_cities_coordinates' => [
    'lat' => $city['lat'],
    'lng' => $city['lng'],
  ],
]);

//set the fields again manually, but this has no effect
$term->field_cities_zip_city->postal_code = $city['zip'];
$term->field_cities_zip_city->locality = $city['name'];

$term->save();

The address fields are still empty. What am I missing?? Thanks in advance for any help!

2
  • 1
    Could it be because you're not setting postal code properly? In your example above you are setting postal code to $city['plz'] which doesn't appear to be defined.
    – sonfd
    Commented May 21, 2018 at 22:41
  • Ah no that‘s a typo for the example :/ Commented May 22, 2018 at 4:35

1 Answer 1

3

You need to provide the country_code to your address field.

like

$city = [
  'zip' => 1000,
  'name' => 'Lausanne',
  'lat' => '47.3879577',
  'lng' => '8.044136',
];


$term = Term::create([
  'name' => $city['zip'].' '.$city['name'],
  'vid' => 'cities',
  'field_cities_zip_city' => [
    'country_code' => 'CH',  // country code is required
    'postal_code' => $city['zip'],
    'locality' => $city['name'],
  ],
]);
$term->save();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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