Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hello,
I'm trying to load a node and load values of the field collections items in that node. I'm trying 'entity_load('field_collection_item', array($ids))' but it doesn't work. Is there a way to load the field values programmatically?
Thanks.
Comments
Comment #2
ashopin CreditAttribution: ashopin at Portage CyberTech commentedI figured out out. I load the field_collection item and then add it's fields and their values to an array:
Comment #3
tim.plunkettI *think* you can skip the ::load() if you do this:
Or something to that extent. ->entity instead of ->value
Comment #5
dudde CreditAttribution: dudde commentedHi I am trying to get my head around how you store and get field collection fields programmatically in drupal 8. This is the only example I could find when googling. Your code helped me but didn't work straight off.
I have a field collection field called "field_fact" and inside i have a text field called "field_title" and also a a multiple text field called "field_sources" and I wish to store values to these programmatically. This is what I've got so far:
But it throws "Fatal error: Class 'Drupal\uthirsty\Form\FieldCollectionItem' not found in" although I've tried using
Is there perhaps some other namespace I should be using?
Thankful for your help!
Comment #6
ashopin CreditAttribution: ashopin commentedI'm using these:
use Drupal\Core\Controller\ControllerBase;
use Drupal\field_collection\Entity\FieldCollectionItem;
The rest of your code looks ok to me.
Comment #7
mrupsidown CreditAttribution: mrupsidown commentedHello, here is how I did it:
Comment #8
Andrew211 CreditAttribution: Andrew211 commentedI'm all for #2 for now, #3 outputs NULL,
Comment #9
Andrew211 CreditAttribution: Andrew211 commentedWorking Example where "field_message" is the collection:
Comment #10
rashid_786 CreditAttribution: rashid_786 commentedworking example:
Comment #11
anouinstead of
$item = $item->value;
it should be
$item = $item->target_id;
Comment #12
Karan Sen CreditAttribution: Karan Sen as a volunteer and commentedTo load the field collection items in node please see the module Field Collection load. The link to the module is https://www.drupal.org/project/field_collection_load.
Comment #13
earlyburg CreditAttribution: earlyburg as a volunteer and commentedSymfony Framework:
$nid = The Node Id.
$bundle_type = The bundle type like "article".
$collection_name = A field collection has a name like a field name. Think if it as a field containing fields.
Iterate through the field collection thusly: