Get Field Collection Fields

Public

Get all fields into an Field_collection

</> CopyGet raw version
php
  1. /**
  2. * search all your field_collection
  3. * @param $node the loaded node
  4. * @param $collection the machine name of your field collection
  5. * @return array of objects FieldCollectionItemEntity
  6. */
  7. function getFieldCollection($node, $collection) {
  8.  
  9. foreach($node->{$collection}[LANGUAGE_NONE] as $collectionItem) {
  10. $entity = entity_load('field_collection_item', array($collectionItem['value']));
  11. $fieldCollection[] = $entity[(int)$collectionItem['value']];
  12. }
  13.  
  14. return $fieldCollection;
  15.  
  16. }

Comments

kalman.hosszu's picture

I think you should use filed_get_items() instead of hardcoded $node->{$collection}[LANGUAGE_NONE] and you should use Drupal Coding Standards.

I would implement like this:

  1. $collections = array();
  2. $field_collection_fields = field_get_items($entity_type, $entity_object, $field_collection_name);
  3. foreach ($field_collection_fields as $field_collection_field) {
  4. // Load the field collection entity...
  5. $collection_entity = entity_load('field_collection_item', array($field_collection_field['value']));
  6. $collections[] = current($collection_entity);
  7. }
ken's picture

Thanks guys, useful code!