0

I know there are other questions similar to this, but I'm having a specific problem. I'm trying to programmatically update a field collection (from it's fid only). This appears to work when printing out the values on the frontend, but when editing the node that the field collection is attached to, the fields within the collection are empty, but the collection itself is there.

// Unset and start afresh
unset($node->field_composition[LANGUAGE_NONE]);

foreach($fids as $fid){
  $comp = entity_load_single('field_collection_item', array($fid));
  $comp->field_chemical[LANGUAGE_NONE][0]['target_id'] = $id; // These values are derived elsewhere and are correct
  $comp->field_weight[LANGUAGE_NONE][0]['value'] = $value; // These values are derived elsewhere and are correct
  $comp->save(TRUE);
  $node->field_composition[LANGUAGE_NONE][] = array('value' => $comp->item_id);
}

node_save($node);

I assume the problem is to do with revisions - as the data is correctly saved somewhere - else it wouldn't appear on the front end. How to solve this?

1

Well, as always happens I found the answer just after I posted the question:

// Unset and start afresh
unset($node->field_composition[LANGUAGE_NONE]);

foreach($fids as $fid){
  $comp = entity_load_single('field_collection_item', array($fid));
  $comp->field_chemical[LANGUAGE_NONE][0]['target_id'] = $id; // These values are derived elsewhere and are correct
  $comp->field_weight[LANGUAGE_NONE][0]['value'] = $value; // These values are derived elsewhere and are correct
  $comp->save(TRUE);
  $node->field_composition[LANGUAGE_NONE][] = array(
    'value' => $comp->item_id,
    'revision_id' => $comp->revision_id // ADDED REVISION ID HERE!
  );
}

node_save($node);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.