6

I have a the following code:

// Create an Entity with wrapper
  $node = entity_create('node', array('type' => 'person'));
  $entity = entity_metadata_wrapper('node',  $node);

  // Add a node title
  $entity->title = $person['name'];

  // Add the address information
  $entity->field_person_address->thoroughfare = $person['address'];
  $entity->field_person_address->locality = $person['city'];
  $entity->field_person_address->administrative_area = $person['state'];
  $entity->field_person_address->postal_code = $person['postalcode'];
  $entity->field_person_address->country = $person['country'];

  // Save the entity
  $entity->save();

The data is saved in the DB perfeclty, but I need to get the entity ID of the new save node.

How do I do that?

10

You can use

$entity->save();

$nid = $entity->nid->value();
23

You can use getIdentifier() method of the wrapper.

$node = entity_create('node', array('type' => 'person'));
$entity = entity_metadata_wrapper('node',  $node);
$entity->save();
$entity->getIdentifier();
  • 1
    Much better than the direct way, good find – Clive Sep 30 '13 at 16:09
  • 2
    Up vote for entities that are not nodes – chadpeppers Sep 23 '14 at 15:38

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.