1

I needed to to add / update field collections to node entities without updating the node entities. I tried two ways listed in https://www.drupal.org/node/1842304 and http://alexrayu.com/blog/saveupdate-field-collection-without-nodesave but none of them seems to be working exactly the way I want.

I tried as follows:

$node = node_load($nid);
$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_college_rating_data'));
$field_collection_item->setHostEntity('node', $node);
$field_collection_item->field_text1['und'][0]['tid'] = $form_state['input']['field_text1']['und'];
$field_collection_item->field_text2['und'][0]['value'] = $form_state['input']['field_text2']['und'];
$field_collection_item->save();

It added a field collection but it updates the node.

I also tried to alter the field collection form submit and used custom submit handler as follows:

function own_custom_field_collection_submit($form,$form_state) {
  $field_collection_item = field_collection_item_form_submit_build_field_collection($form, $form_state);
  $field_collection_item->save(TRUE);
  drupal_set_message(t('The changes have been saved.'));
  $form_state['redirect'] = $field_collection_item->path();
}

I have copied this code from core field collection module to change the default argument to "TRUE" in the save function. It added the field collection but didn't associated with the parent node.

I need to save the field collection separately since my node entity form is very large with 50 to 60 fields and field collections and I don't want to update it as many times as I add / update any field collections to the node.

Any help?

0

You can make use of the Entity API

example will be

<?php
// Implements hook_entity_update().
function qmlc_mod_entity_update($entity, $type) {
  if ($type == 'node' && $entity->type == 'event') {
    $link_title = $entity->title;
    $link_url = entity_uri('node', $entity);
    $language = $entity->language;

    foreach ($entity->field_event_images[$language] as $event_images) {
      // Load field collection item.
      $field_collection = field_collection_field_get_entity($event_images);

      $num_of_images = count($field_collection->field_image[$language]);
      for ($i = 0; $i < $num_of_images; $i++) {
        $field_collection->field_image_link[$language][$i]['title'] = $link_title;
        $field_collection->field_image_link[$language][$i]['url'] = $link_url['path'];
        $field_collection->field_image_link[$language][$i]['attributes']= array();
      }

      // Save field collection item. Use TRUE since you are updating a field collection item which is already linked with a node.
      $field_collection->save(TRUE);
    }
  }
}
?>

References

  1. Programmatically create and update field collection with Entity API
  2. Update field collection programmatically
  • The code is for updating field collection when node entity is saved. But what if I want to add only field collection to a node without updating the node. – viswanath polaki Jun 8 '15 at 6:16

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.