Load field collection items programmatically?

Created on 30 December 2015, over 8 years ago
Updated 26 July 2023, 11 months ago

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.

πŸ’¬ Support request
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada ashopin

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States earlyburg

    Symfony Framework:

    public function getFieldCollectionByNid($nid, $bundle_type, $collection_name) {
        $field_collection = FALSE;
        $node = \Drupal::service('entity_type.manager')->getStorage('node')->load($nid);
        if ($node->bundle() == $bundle_type) {
          $field_collection = $node->get($collection_name);
        }
        return $field_collection;
      }
    

    Iterate through the field collection thusly:

        foreach ($field_collection as $field) {
          $item = \Drupal::service('entity_type.manager')->getStorage('field_collection_item')->load($field->target_id);
        }
    
Production build 0.69.0 2024