Search code examples
phpdrupaldrupal-7entity

Can't save node after setting its filedcollection DRUPAL


Have such structure: Node -> Filed "field_server_supported_hardware" (Fieldcollection) -> Field "field_hardware_items" (Node reference width checkboxlist)

I want to add one more item in "field_hardware_items" (check one more item in ckeckboxlist).

Here's my code, it doesn't throws errors, but I can't save changes:

$node_wrap = entity_metadata_wrapper('node', $node_id); // our node wrapper

$node_wrap->field_server_supported_hardware[$key_item]->field_hardware_items->offsetSet($new_key, $new_node_item_to_attach);  // setting new item

___save_debug($node_wrap->field_server_supported_hardware[$key_item]->field_hardware_items->value()); // if I log "field_hardware_items", it includes new item, and includes it in correct structure.
$node_wrap->save(TRUE);  // trying to save node

Then I open the node I saved in admins panel, but checkbox of node with id=$new_node_item_to_attach isn't checked.


Solution

  • Give $node_wrap->revision->set(TRUE) a try before saving it Apparently the save() function don't take into account the flag to create a new revision on a node on update, so it may be why you don't see the change.