I am trying to change a field value inside both hook_node_insert()
and hook_node_update()
using EMW. It actually changes the original $node
object that is passed to these hook functions but the change doesn't get saved at the end.
function mymodule_node_insert($node) {
if (!_is_dynamic_type($node->type))
return;
$messagew = entity_metadata_wrapper('message', message_create('post', array('uid' => $node->uid)));
//copy terms
$nodew = entity_metadata_wrapper('node', $node);
//below loads a full taxonomy term
$nodew->field_hidden_type = taxonomy_term_machine_name_load('event', 'hidden_type');
$messagew->field_hidden_type = $nodew->field_hidden_type->value();//this actually works
$messagew->save();
}
There are a few questions near to this one actually, but none of them addresses this precisely. This one for example is a very good explanation of the mechanism but its suggestion it not use.
Here, one should not use $nodew->save(), because it causes infinite loop. I also played a little with module weight, that does not help either.
I also tried to change another entity reference field. No use.
I don't want to go with field_attach_update()
because it is old-fashioned.
hook_node_presave()