Modifying a field collection programmatically missing hostEntity fields

10.1k views Asked by At

I am trying to modify a field collection in a node that already exists so I can change an image on the first element in an array of 3. The problem is, the hostEntity info is not set when I do a entity_load or entity_load_single so when I do a:

$field_collection_item->save(true); // with or without the true
// OR
$fc_wrapper->save(true); // with or without the true

I get the following error:

Exception: Unable to save a field collection item without a valid reference to a host entity. in FieldCollectionItemEntity->save()

When i print_r the field collection entity the hostEntity:protected fields are indeed empty. My field collection is setup as follows:

  • field_home_experts
    1. Expert Image <--- Want to change this data only and keep the rest below
      • field_expert_image
      • Image
    2. Expert Name
      • field_expert_name
      • Text
    3. Expert Title
      • field_expert_title
      • Text

Here is the code I am trying to use to modify the existing nodes field collection:

$node = getNode(1352); // Get the node I want to modify

// There can be up to 3 experts, and I want to modify the image of the first expert
$updateItem = $node->field_home_experts[LANGUAGE_NONE][0];

if ($updateItem) { // Updating
    // Grab the field collection that currently exists in the 0 spot
    $fc_item = reset(entity_load('field_collection_item', array($updateItem)));

    // Wrap the field collection entity in the field API wrapper
    $fc_wrapper = entity_metadata_wrapper('field_collection_item', $fc_item);

    // Set the new image in place of the current
    $fc_wrapper->field_expert_image->set((array)file_load(4316));

    // Save the field collection
    $fc_wrapper->save(true);

    // Save the node with the new field collection (not sure this is needed)
    node_save($node);
}       

Any help would be greatly appreciated, I am still quite new to Drupal as a whole (end-user or developer)

2

There are 2 answers

1
JakeTheBraek On BEST ANSWER

Alright so I think I have figured this out, I wrote up a function that will set a field collection values:

// $node: (obj) node object returned from node_load()
// $collection: (string) can be found in drupal admin interface: 
//              structure > field collections > field name
// $fields: (array) see usage below
// $index: (int) the index to the element you wish to edit           

function updateFieldCollection($node, $collection, $fields = Array(), $index = 0) {
    if ($node && $collection && !empty($fields)) {
        // Get the field collection ID
        $eid = $node->{$collection}[LANGUAGE_NONE][$index]['value'];

        // Load the field collection with the ID from above
        $entity = entity_load_single('field_collection_item', array($eid));

        // Wrap the loaded field collection which makes setting/getting much easier
        $node_wrapper = entity_metadata_wrapper('field_collection_item', $entity);

        // Loop through our fields and set the values
        foreach ($fields as $field => $data) {
            $node_wrapper->{$field}->set($data);
        }

        // Once we have added all the values we wish to change then we need to 
        // save. This will modify the node and does not require node_save() so
        // at this point be sure it is all correct as this will save directly
        // to a published node 
        $node_wrapper->save(true);
    }
}

USAGE:

// id of the node you wish to modify
$node = node_load(123); 

// Call our function with the node to modify, the field collection machine name
// and an array setup as collection_field_name => value_you_want_to_set
// collection_field_name can be found in the admin interface:
// structure > field collections > manage fields
updateFieldCollection(
    $node,
    'field_home_experts',
    array (
        'field_expert_image' => (array)file_load(582), // Loads up an existing image
        'field_expert_name' => 'Some Guy',
        'field_expert_title' => 'Some Title',
    )
);

Hope this helps someone else as I spent a whole day trying to get this to work (hopefully I won't be a noob forever in Drupal7). There may be an issue getting formatted text to set() properly but I am not sure what that is at this time, so just keep that in mind (if you have a field that has a format of filtered_html for example, not sure that will set correctly without doing something else).

Good luck! Jake

1
Daniel Tome On

I was still getting the error, mentioned in the question, after using the above function. This is what worked for me:

function updateFieldCollection($node, $collection, $fields = Array(), $index = 0) {
  $eid = $node->{$collection}[LANGUAGE_NONE][$index]['value'];
  $fc_item = entity_load('field_collection_item', array($eid));
  foreach ($fields as $field => $data) {
    $fc_item[$eid]->{$field}[LANGUAGE_NONE][0]['value'] = $data;
  }
  $fc_item[$eid]->save(TRUE);
}

I hope this helps someone as it took me quite some time to get this working.