2

I have been trying to learn the ins and outs of hooks and basic theming in Drupal.

I currently have a template file for one of my content type (node--opportunities.tpl.php). Everything has been going great theming with this except for a field collection that I am now stuck on. I know there are a multiple ways of theming a field collection and this is the worse. But I can't figure it out when the array is perfectly right in front of me.

field_services is the field name. When I dsm($content) on the node you can see it perfectly. So I'm using a foreach to loop through the items, pull them out and load them. Then print their values with my markup. But it will not render. Even when I 'dsm' the final array, it shows me my fields perfectly. But when I try to render them, nothing happens.

Can anyone help me understand what I am doing wrong? I am more interested in why my investigative attempts to print the values of this array are fruitless than I am interested in actually theming the field collection.

    hide($content['field_services']); 
    //horizontal radios
    print '<div class="ec-controls">';
      print render($content['field_opportunity_type']);
    print '</div>';


    //if (!empty($content['field_services'])) {


        print '<div class="ec-collection">';


         foreach ($content['field_services']['#items'] as $entity_uri) {


          $a_field_collection_item = entity_load('field_collection_item',                 $entity_uri);


            foreach ($a_field_collection_item as $a_field_collection_item_object ) {


              dsm($a_field_collection_item_object); 


              print render($a_field_collection_item->field_service);
              print render($a_field_collection_item_object->field_service_multiplier);
              print render($a_field_collection_item_object->field_extend_service_total);


              //print render($a_field_collection_item_object->field_service['und'][0]);
              //$...->field_service['und'][0]['entity']->field_unit_price
              //->field_service['und'][0]       
            }
        }
        print '</div>' . "\n"; 
    //}

I've also tried these as suggested with no luck. Here is also a printscreen of the dpm:

DPM PS http://ecteam.net/Untitled.png

    dpm($a_field_collection_item_object);

    print 'test';
    print $a_field_collection_item->field_service['und'][0];
    print $a_field_collection_item->field_service['und'][0]['entity']->title;
    print $a_field_collection_item->field_service['und'][0]['target_id'];
    print $a_field_collection_item->field_service_multiplier['und'][0]['value'];
    print $a_field_collection_item->field_service_multiplier['und'][0]['value'];
    print $a_field_collection_item->field_extend_service_total['und'][0]['value'];

Note: field_services is the parent field collection, field_service is the individual field that contains the service title that I am trying to render from it.

2
  • You state field_services is the field name, your code is using field_service.
    – Shawn Conn
    Commented Sep 12, 2015 at 18:10
  • field_services is the parent field collection, field_service is the individual field that contains the service title that I am trying to render from it. Commented Sep 12, 2015 at 21:07

2 Answers 2

1

Arpitr was correct but I was also referencing the wrong variable.

The foreach loops states foreach ($a_field_collection_item as $a_field_collection_item_object ) {

I guess I just wasnt paying attention. I used this:

    print render($a_field_collection_item->field_service);

but I should have used this:

    print render($a_field_collection_item_object ->field_service);

Final solution for how to theme a field collection in a node template

My content is called opportunities. So in my node--opportunities.tpl.php I put:

    if (!empty($content['MY_FIELD_COLLECTION'])) {
      hide($content['MY_FIELD_COLLECTION']);

      print '<div class="ec-collection">';

      foreach ($content['MY_FIELD_COLLECTION']['#items'] as $entity_uri) {

        $a_field_collection_item = entity_load('field_collection_item', $entity_uri);

            foreach ($a_field_collection_item as $a_field_collection_item_object ) {

                print $a_field_collection_item_object->field_service['und'][0]['entity']->title;
                print $a_field_collection_item_object->field_service_multiplier['und'][0]['value'];
                print $a_field_collection_item_object->field_extend_service_total['und'][0]['value'];

            }
    }
   print '</div>' . "\n"; 
   }
0

You do not need to call render function. render() is for renderable arrays.

In your case change from

print render($a_field_collection_item->field_service);

to

print $a_field_collection_item->field_service['und'][0]['value'];

similarly for others fields, do a dpm($a_field_collection_item); to ensure you pick the right keys for arrays.

For example if service is text field then to fetch you will use print $a_field_collection_item->field_service['und'][0]['value']; if its entity reference field than print $a_field_collection_item->field_service['und'][0]['target_id'];

1
  • THanks thats great advice but still not working. I tried to do that and a few other variations. Looks right compared to dpm, but still oddly is not printing anything out?? Here is a printscreen of the dpm. ecteam.net/Untitled.png here is the code ecteam.net/code.png Commented Sep 13, 2015 at 2:25

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.