What am I doing wrong here? This code works when I'm updating things like the node title, but not working when I update a field within a collection.

Background, 1 collection named otu_password with 3 fields, (1 field named otu_name (text) within that collection that I'm trying to update with php). The updated value is not saving. When I go to the node edit form the old value is still displayed. Any ideas?

    //Load Node
    $updateNode = node_load($nid);
    
    //Setup Wrapper
    $wrapper = entity_metadata_wrapper('node', $updateNode);
    
    //Output Current Value (Works)
    dsm($wrapper->field_otu_password[0]->field_otu_name->value());
    
    //Chnage Value (Tried Both ways)
    $wrapper->field_otu_password[0]->field_otu_name = 'New Value';
    /*$wrapper->field_otu_password[0]->field_otu_name->set('New Value');*/ 
    
    //Save New Value
    $wrapper->save();
    
    //Output New Value (Works)
    dsm($wrapper->field_otu_password[0]->field_otu_name->value());
    
    //Save Updated Node
    node_save($updateNode);

Comments

timefor’s picture

Status: Active » Fixed

Solved - I found this article,

http://rajanmayekar.com/blog/programmatically-creating-deleting-modifyin...

It didn't work exactly as expected but it lead me to working code. Looks like entity_load returns an array of objects instead of the object itself now.

<?php

    $field_collection_item_id = 15; //Had to find the item id on my own
    $field_collection_item = entity_load('field_collection_item', array($field_collection_item_id));
    $field_collection_item[$field_collection_item_id]->field_otu_name[LANGUAGE_NONE][0]['value'] = 'New Value 5';
    $field_collection_item[$field_collection_item_id]->save();

?>

I messed with this too long, hope this helps someone.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

added more details.