The following code to copy fields from one entity to another fails to do anything unless the commented-out line is restored:
$node1 = entity_metadata_wrapper('node', $nid1);
$node2 = entity_metadata_wrapper('node', $nid2);
foreach ($node1->{$field_name} as $delta => $field) {
// Calling a function such as getIdentifier() forces the field entity to be fully loaded -- making code work
// $field->getIdentifier();
$node2->{$field_name}[] = $field;
}
A long afternoon of debugging revealed that the cause of the problem is that when EntityDrupalWrapper::setEntity() is called for a wrapped entity, it never checks that the source entity has been fully loaded -- it copies $data->id even though $data->id has never been set.
Comments
Comment #1
chris matthews commentedThe 6 year old patch to entity.wrapper.inc applied cleanly to the latest entity 7.x-1.x-dev, but still needs review.