Tuesday 25 August 2015

Field Collections

Drupal 7- How to get field collection item value for a node


I have a node that has a few field collections on it. When I load the node it gives...
$node = node_load(369);
print_r($node)
Class Object
(
    [vid] => 370
    [uid] => 1187
    [title] => Contacts Approval
    [log] => 
    [status] => 1
    [comment] => 1
    [promote] => 0
    [sticky] => 0
    [nid] => 369
    [type] => contacts_approval
    [language] => und
    [created] => 1395819523
    [changed] => 1395819523
    [tnid] => 0
    [translate] => 0
    [revision_timestamp] => 1395819523
    [revision_uid] => 1187
    [field_contacts] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => 474
                            [revision_id] => 472
                        )

                    [1] => Array
                        (
                            [value] => 475
                            [revision_id] => 473
                        )

                )

        )

    [rdf_mapping] => Array
        (
            [rdftype] => Array
                (
                    [0] => sioc:Item
                    [1] => foaf:Document
                )

            [title] => Array
                (
                    [predicates] => Array
                        (
                            [0] => dc:title
                        )

                )

            [created] => Array
                (
                    [predicates] => Array
                        (
                            [0] => dc:date
                            [1] => dc:created
                        )

                    [datatype] => xsd:dateTime
                    [callback] => date_iso8601
                )

            [changed] => Array
                (
                    [predicates] => Array
                        (
                            [0] => dc:modified
                        )

                    [datatype] => xsd:dateTime
                    [callback] => date_iso8601
                )

            [body] => Array
                (
                    [predicates] => Array
                        (
                            [0] => content:encoded
                        )

                )

            [uid] => Array
                (
                    [predicates] => Array
                        (
                            [0] => sioc:has_creator
                        )

                    [type] => rel
                )

            [name] => Array
                (
                    [predicates] => Array
                        (
                            [0] => foaf:name
                        )

                )

            [comment_count] => Array
                (
                    [predicates] => Array
                        (
                            [0] => sioc:num_replies
                        )

                    [datatype] => xsd:integer
                )

            [last_activity] => Array
                (
                    [predicates] => Array
                        (
                            [0] => sioc:last_activity_date
                        )

                    [datatype] => xsd:dateTime
                    [callback] => date_iso8601
                )

        )

    [cid] => 0
    [last_comment_timestamp] => 1395819523
    [last_comment_name] => 
    [last_comment_uid] => 1187
    [comment_count] => 0
    [name] => yogesh
    [picture] => 0
    [data] => a:1:{s:7:"contact";i:0;}
)



Here we can get the field collections item id or entity id using $node with the help of enitity apis -:

<?php
$node = node_load(369); 
$items = field_get_items('node', $node, 'field_contacts');
//here items are having the entity id/itemid
foreach ($items as $item) {
 $fc = field_collection_field_get_entity($item); // Do something. 
}
?>


Note -:

field_get_items() - : It gives all the related entity id and revision id for the field collection . Here our field collection name is "field_contacts";

field_collection_field_get_entity()-: This entity api helps us to get all the relative information for the entity id in all the related tables.