1

This is driving me crazy it just sounds simple, but I cannot find the answer.

I have a field collection in a content type called field_collection_mine how you now while creating the node you can add elements to the field collection.

I have tried this:

$wrapper = entity_metadata_wrapper($entity_type, $entity);
$wrapper->field_collection_mine = array();
entity_save('field_collection_item',$wrapper);

The code above delete all the elements within the field collection, but that is now what I want!! I just want to delete for example the last element. E.g:

  • Field Collection mine
  • Element1
  • Element2
  • Element3

How can I for example delete just the element 3? The above code deletes all the elements, so field_collection_mine is left empty.

  • Check @milkovsky answer here drupal.stackexchange.com/questions/11062/… I think it is what you want – Codium Jan 31 '14 at 19:57
  • I already can access to the items of the field collection! this one for access to the first item $wrapper_egresos->field_salida_mercanc_a[0] but when I try to delete the item with unset, then in the node I cannot see the items but if I do a dpm to the array of items I can still see the other items, but they are not displayed. help! – SsouLlesS Jan 31 '14 at 20:58
1

You need to grab the id of the field collection and wrap itself in an entity wrapper. Use the save method in the wrapper to do this instead of the entity_save function. E.g.

 $wrapper = entity_metadata_wrapper('field_collection_item', $entity);
 $wrapper->field_specific_field_collection_item->set("");

or:

$wrapper->field_specific_field_collection_item = array();
$wrapper->save();
  • Look I do not with what I should replace 'field_specific_field_collection_item' as the unic thing I have is the machine name of my field collection 'field_collection_mine'. – SsouLlesS Jan 31 '14 at 19:46
  • The field collection can contain unlimited number of items, where each item has two fields (date and quantity) how can I delete the last added item? – SsouLlesS Jan 31 '14 at 19:52
1

Finally I could achieve what I wanted. What I did was this:

In my case I wanted to delete the last item within the field collection.

Take a look to this code, for beginners: remember that you must have the $entity already loaded, e.g.

// Remove the field value
unset($entity->field_salida_mercanc_a[LANGUAGE_NONE][count($entity->field_salida_mercanc_a[LANGUAGE_NONE])-1]);

// Reset the array to zero-based sequential keys
$entity->field_salida_mercanc_a[LANGUAGE_NONE] = array_values($entity->field_salida_mercanc_a[LANGUAGE_NONE]);

// Save the entity
entity_save($entity_type, $entity);

Thats all! The other way is:

// Get the node wapper
$wrapper = entity_metadata_wrapper($entity_type, $entity);
// Get the last position of the array items, thanks to the ->value() statement you can get the complete Array properties.
$field_collection_item_value = $wrapper->field_collection_mine[count($wrapper->field_collection_mine->value())-1]->value();
// Do not use 'unset' to delete the item due to is not the correct way, I use the entity_delete_multiple provided by the entity API
entity_delete_multiple('field_collection_item', array($field_collection_item_value->item_id));

The way above is good using entity_metadata_wrapper function, but with that way there is a complex bug that I do not know how to solve it, you can check it at #1880312 and after apply the patch in #9 you get the next issue.

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.