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!

| improve this question | | | | |
  • 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 May 21 '18 at 22:41
  • Ah no that‘s a typo for the example :/ – Florian Müller May 22 '18 at 4:35
1

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();
| improve this answer | | | | |

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.