0

I am trying to print the values from a field collection using following code in Drupal 7 node.But the date values are not getting.

$wrapper = entity_metadata_wrapper('node', $node);
 $formtype = field_get_items('node', $node, 'field_latest_schedules');
 foreach($formtype as $itemid) { 

 $item = field_collection_field_get_entity($itemid);

 print $item->field_course_starts_on['und'][0]['safe_value'];
 print $item->field_course_ends_on['und'][0]['safe_value'];

 }

How can i print the date from a filed collection?

2 Answers 2

0

I suppose that field_course_starts_on and field_course_starts_on are fields that represent date and you want to print it as date. Correct me if I'm wrong.

So you can print those fields as date like:

print date('d F, Y', $item->field_course_starts_on['und'][0]['safe_value']);
print date('d F, Y', $item->field_course_ends_on['und'][0]['safe_value']);

The part 'd F, Y' is a date format. Take a look at PHP Documentation to see all options for format parameter.

Update

First you have to load the ids of the field collection fields of the node:

$formtype = field_get_items('node', $node, 'field_latest_schedules');

// Extract the field collection item ids
$ids = array_column($formtype, 'value');
foreach ($formtype as $ft_field) {
    $ids[] = $ft_field['value'];
}

To iterate over all fields you can use the following code:

//Get the entities
$collections = field_collection_item_load_multiple($ids);

//Loop over the array - if you have more than one entity
for ($i = 1; $i <= sizeof($collections); $i++) {
    $collection = $collections [$i];

    //Iterate over all member vars of the FieldCollectionItemEntity object
    foreach($collection as $key => $value) {
        //Look for fields - there are some other class vars, too.
        if (strpos ($key, 'field') !== false) {
            //Search for textfields 
            if (isset($value['und']) && is_array($value ['und']) && isset($value['und'][0]['value'])) {
                print "Field ".$key." ".$value['und'][0]['value'];
            }
        }
    }                   
} 

And than debug, target wanted fields and print those fields' values.

6
  • i am sorry i tried it and not getting any result.'field_latest_schedules' is my main field name and field type is "field collection".The date fields inside the collections are 'field_course_starts_on' and 'field_course_ends_on' .The manage display type is plain.and i am using the above code to print the values.But date is not coming.How can i print it?
    – harikris
    Commented Apr 25, 2015 at 5:21
  • Do you get any output when you print those two fields as you do and what do you get?
    – mixerowsky
    Commented Apr 25, 2015 at 10:27
  • I think on output without date function (your originally code), what you get as output?
    – mixerowsky
    Commented Apr 25, 2015 at 10:39
  • print $item->field_course_starts_on['und'][0]['safe_value'] ; this shows empty result...
    – harikris
    Commented Apr 25, 2015 at 10:47
  • I update my answer...
    – mixerowsky
    Commented Apr 25, 2015 at 11:05
0

This is working for me....thanks for the helps....

<?php

if (isset($content['field_latest_schedules']['#items'])) {

    foreach ($node->field_latest_schedules['und'] as $key => $value) {
    $field_collection = entity_load('field_collection_item', array($value['value']));
    $idx = $value['value'];

    print $field_collection[$idx]->field_course_starts_on['und'][0]['value'];
    print $field_collection[$idx]->field_course_ends_on['und'][0]['value'];
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.