I'm using Drupal 7 and the entity module, I have a contenttype setup called property, what I'm attempting to do is to create content types from a xml file, I have the correct data and I'm using the entity_metadata_wrapper to simplify insertion into the fields:
Here is the method I'm using:
private function newProperty($propValues) {
global $user;
$values = array(
'type' => 'property',
'uid' => $user->uid,
'status' => 1,
'comment' => 1,
'promote' => 0,
);
$entity = entity_create('node', $values);
$ewrapper = entity_metadata_wrapper('node', $entity);
$ewrapper->title->value=$propValues->price_text;
$ewrapper->field_property_expert_agent_ref->value =$this->xml_attribute($propValues, 'reference');
foreach ($this->valuesToFetch() as $key=>$value) {
$ewrapper->{$value}->value=$propValues->$key;
}
$ewrapper->save();
entity_save('node', $entity);
}
$propValues holds an array of values $this->valuesToFetch() is a key=>value array eg : 'department'=>'field_property_department', I have added debugging code to confirm that the values are coming through, the ct gets created but its values are empty.
What am I doing wrong?
In order to set field values using entity_metadata_wrapper you must to use ->set()
method or rely the magic methods which can offer more code clarity.
// 1. Using ->set()
// Single value
$wrapper->field_data->set($value);
// Multi value
$wrapper->field_data[]->set($value); // Add to field array
$wrapper->field_data[$delta]->set($value); // Set specific value
// 2. Using magic methods
// Single value
$wrapper->field_data = $value
// Multi value
$wrapper->field_data[] = $value; // Add to field array
$wrapper->field_data[$delta] = $value; // Set specific value
// 3. Deleting values
// 'Unset' a field value (there is no delete method)
$wrapper->field_data->set(NULL);
$wrapper->field_data = NULL;
Drupal 7 Entity metadata wrappers documentation. A good blog post that has some useful insights!