I am having a custom entity called Micronews which has a bundle microupdate created in it. I am able to write entity to db for all fields but I am not able to figure out how to write location (using location module). It seems to be stored in db in multiple tables. Is there any standard way to store location to db?

function p_save_entity($name, $issue_type, $constituency, $location){

$name = "Testing programmatically";
$issue_type = "electricity";
$constituency = '22';

$e = entity_create('micronews', array(
    'type' => 'microupdate',
    'created'=> time(),


$ew = entity_metadata_wrapper('micronews' ,$e);



$fl = $ew->save();

if ($fl){
    return "";

    return "Something went wrong";

I am using this module.

3 Answers 3


This works, but (here's the trick), only if you enable the location_entity module, which ships with location.

$location = array(
  'latitude' => $latitude,
  'street'   => $street,
  // etc, other elements added here ...


May no one else lose as much time on this ever again.

  • I get: EntityMetadataWrapperException: Unknown data property field_location. in EntityStructureWrapper->getPropertyInfo() (line 354 of /var/www/sites/all/modules/entity/includes/entity.wrapper.inc). Commented Apr 28, 2017 at 19:05

I would reference location.test in the modules folder. Specifically something like testCreateLocation().

That code does the following for a location field:

 135     $node = $this->drupalCreateNode(array(
 136       'type' => $location_type,
 137       'field_loctest' => array(
 138         array(
 139           'name' => $location1_name,
 140           'street' => '48072 289th St.',
 141           'province' => 'SD',
 142           'country' => 'us',
 143   //        'location_settings' => $settings,
 144         ),
 145       ),
 146     ));
 147     cache_clear_all();
 149     // Reload the node.
 150     $node2 = node_load($node->nid, NULL, TRUE);
 151     $location = $node2->field_loctest[0];
 153     $this->assertEqual($location1_name, $location['name'], t('Testing basic save/load'));

Using an entity wrapper it might be like (given that Location can be from different pieces of data, the test uses Street, your using Lat/Longs):

$ew->field_location->set(array(array('latitude' => 65.000000, 'longitude' => 34)));

I've used EntityAPI and wrappers before, but I dont do it all the time to have it all memorized. I found this other link helpful: What is the purpose of the entity_metadata_wrapper() function, and why should I use it?

If you follow the links on the above EntityAPI thread you see they build a $profile_object using nested arrays ...

  • Thanks for response. $ew->field_location->set() is giving error "Unknown data property field_location". I dont know what is going wrong. I also tried the solution from this link where the comment on accepted answer is for drupal 7. stackoverflow.com/questions/3945898/… but got error. Could you help some more please. Thank you. Commented Feb 1, 2013 at 12:05
  • sorry, not sure how to help more. I've tried to help by pointing you to docs that give examples. if you can export the field defintion and update your post to show the field config array maybe that would help ...
    – tenken
    Commented Feb 1, 2013 at 13:52

Late response, but figured it'll help someone.

The "unknown data property field_location" happens to me as well. I thought it was due to the entity not yet having been initialized. But creating the entity and resetting the wrapper had no effect. The following workaround solves the problem:

$entity->{$drupal_field}['und'][0] = $location;
// This seems to be necessary
$entity->locations[0] = $location;
$resp = entity_save('node', $entity);
if ($resp == false) {
    // do something

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.