Normally, if you want to programmatically set a field value on an entity, you can just use a snippet like the following...

<?php
$wrapper = entity_metadata_wrapper($entity_type, $entity_id);
$wrapper{$field_name}->set($field_value);
?>

However, this doesn't work for location fields and results in an "unknown data property" error, because the field doesn't even exist on the entity wrapper. The following code will print all of the fields on the entity except location fields.

<?php
$wrapper = entity_metadata_wrapper($entity_type, $entity_id);
dpm($wrapper->getPropertyInfo());
?>

I'm not the first person to run into this:
http://drupal.stackexchange.com/questions/59195/how-do-i-write-to-locati...

Comments

jakeschlachter’s picture

Title: Field not returned by entity_metadata_wrapper » Move entity_metadata_wrapper support into main location module
Related issues: +#1186868: [Needs Tests]Integration with Entity API, Search API, +#1271372: Please describe how to create a location field programatically

This actually works the way you expect, but only if you first enable the location_entity module.

I wasted a lot of time learning that. I'm updating this bug report to instead request that this behavior be moved into the main location module so it will be available by default. I believe this is more consistent with the principle of Least Surprise.

For anyone else struggling with this, check out the referenced stackexchange question.

michielkenis’s picture

I'ld like to bump this issue and confirm the location_entity module is required in order to get this to work!
Thanks jakeschlachter for the info!

Chris Charlton’s picture

Odd, I have the Location Entity contrib module enabled but I cannot see the location field/array at all yet even when I use the following code below. Thoughts?

$entity = entity_metadata_wrapper('node', $node);
dpm($entity->getPropertyInfo()); // No 'location' or 'locations' field accompanying the entity wrapper object