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

ashopin created an issue. See original summary.

ashopin’s picture

I figured out out. I load the field_collection item and then add it's fields and their values to an array:

foreach ($node->$field_collection as $key => $item){
          $item = $item->value;
          $fcArray[$key];
          $fc = FieldCollectionItem::load($item);
          foreach ($fc as $fckey => $field){
            $fcArray[$key][$fckey] = $field->value;
          }
        }
tim.plunkett’s picture

Status: Active » Fixed

I *think* you can skip the ::load() if you do this:

$fcArray = [];
foreach ($node->$field_collection as $key => $item){
  $fcArray[$key] = [];
  foreach ($item->entity as $fckey => $field){
    $fcArray[$key][$fckey] = $field->value;
  }
}

Or something to that extent. ->entity instead of ->value

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

dudde’s picture

Hi 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:

      $destinationNode = \Drupal\node\Entity\Node::load($nid['destination']);
      $field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_fact'));
      $field_collection_item->setHostEntity($destinationNode);

      $fcArray = [];
      foreach ($field_collection_item as $key => $item){

        $item = $item->value;
        $fcArray[$key];
        $fc = FieldCollectionItem::load($item);
        foreach ($fc as $fckey => $field){
          $fcArray[$key][$fckey] = $field->value;
        }

But it throws "Fatal error: Class 'Drupal\uthirsty\Form\FieldCollectionItem' not found in" although I've tried using

use Drupal\field_collection;
use Drupal\field_collection/Entity;

Is there perhaps some other namespace I should be using?

Thankful for your help!

ashopin’s picture

I'm using these:

use Drupal\Core\Controller\ControllerBase;
use Drupal\field_collection\Entity\FieldCollectionItem;

The rest of your code looks ok to me.

mrupsidown’s picture

Hello, here is how I did it:

$field_collection = $node->get('field_collection_field');
$fc_array = [];

foreach ($field_collection as $fc) {

  $fc_array[] = \Drupal::entityTypeManager()
    ->getViewBuilder('field_collection_item')
    ->view($fc->getFieldCollectionItem());
}
Andrew211’s picture

I'm all for #2 for now, #3 outputs NULL,

foreach ($node->$field_collection as $key => $item){
  $test = $item->entity;
  var_dump($test); //outputs NULL
}
Andrew211’s picture

Working Example where "field_message" is the collection:

foreach($node->field_message as $key => $item){
  $item = $item->value;
  $fcArray[$key];
  $fc = \Drupal\field_collection\Entity\FieldCollectionItem::load($item);
  foreach ($fc as $fckey => $field){
    $fcArray[$key][$fckey] = $field->value;
   }
}
print_r($fcArray);
rashid_786’s picture

working example:

foreach($node->field_sub_traders as $key => $item){
	  $item = $item->value;
	  $fc = \Drupal\field_collection\Entity\FieldCollectionItem::load($item);	  
	  $fcArray[$key]['trader_name'] = $fc->get('field_trader_name')->getValue();	  
	  $fcArray[$key]['contact_no'] = $fc->get('field_contact_no')->getValue();
	 
    }
	
    print "<pre>";print_r($fcArray);die;
anou’s picture

instead of $item = $item->value;
it should be $item = $item->target_id;

Karan Sen’s picture

To 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.

earlyburg’s picture

Symfony 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.

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);
      $itemFields = $item->getFields();
      foreach ($itemFields as $fieldContents) {
        dsm($fieldContents->getName());
        dsm($fieldContents->getValue());
      }
    }