7

I've been playing around with this for the better part of a few days now with no luck. Here is my Collection data:

Collection name: field_video_collection

Within the collection there is a field called: field_video_file_col

My fields print fine in my custom field-collection-item--field_video_collection.tpl.php template, I followed the documentation according to this issue to get my template name and I simply print the fields as <?php print render($content['field_video_file_col']); ?> etc... within that template.

Now for the fun part, I'd like to extract the fid for each field_video_file_col item in the collection in any given node and render it as a printed variable. I followed the answer in this question here on Stack but it does not seem to work for me. if I do:

print '<pre>';
var_dump(get_defined_vars());
print '</pre>';

I see I have this as part of the data available to me for field_video_file_col

["field_video_file_col"]=>
          array(1) {
            ["und"]=>
            array(1) {
              [0]=>
              array(12) {
                ["fid"]=>
                string(3) "888"

So it looks to me that the FID is 888 in this case and I want to render that in my template. My node preprocess function is as such:

function MYTHEME_preprocess_node(&$vars, $hook) {

    $entity_type = 'field_collection_item';
    $bundle_name = 'field_field_video_collection'; // Field name the collection is attached to
    $field_name = 'field_video_file_col'; // Field name within the field collection
    $video_file_fid = field_info_instance($entity_type, $field_name, $bundle_name);
    if ($video_file_fid) {
      $my_fid = $video_file_fid['fid'];
    }
}

The end result needed is that I need to render the variable in my field-collection-item--field_video_collection.tpl.php template as:

<?php print $video_file_fid; ?>

... but this does not seem to work. I've searched all over and tried many different incarnations of this code and combinations but still no joy.

  • You changed MYTHEME to your theme name right? :) – Lance Apr 5 '13 at 10:44
  • @Lance - yes I changed it. – Danny Englander Apr 6 '13 at 15:55
8
+25

I use Entity API when work with Field Collection Items. See: Programmatically create and update field collection with Entity API

To work with field collections can do smth like this:

// Wrap node with Entity API
$node_wrapper = entity_metadata_wrapper('node', $node);
// Get the first item from the muli-value field collection
$raw_collection = $node_wrapper->field_video_collection[0]->value();
// Wrap it with Entity API
$collection = entity_metadata_wrapper('field_collection_item', $raw_collection);
//dsm the old value
debug($collection->field_video_file_col->value());

If you already know your field_collection id you can just do:

$collection = entity_metadata_wrapper('field_collection_item', $fid);
//dsm the old value
debug($collection->field_video_file_col->value());

UPDATE:

use the code above in MYTHEME_preprocess_node

UPDATE 2:

So in your case it will be:

function MYTHEME_preprocess_node(&$vars, $hook) {
  if (empty($vars['nid'])) {
    return;
  }
  // Wrap node with Entity API
  $node_wrapper = entity_metadata_wrapper('node', $vars['nid']);
  // Get the first item from the muli-value field collection
  $raw_collection = $node_wrapper->field_video_collection[0]->value();
  // Wrap field collection with Entity API
  $collection = entity_metadata_wrapper('field_collection_item', $raw_collection);
  // print value
  $fid = $collection->field_video_file_col->value();
  debug($fid);
}

Also you can find answer at Understanding Field Collection Values

  • I follow your code above somewhat but I don't see how this creates and renders a variable in my custom field collection template so I am not sure if this answer has any practical use for me. – Danny Englander Apr 4 '13 at 15:08
  • 1
    you would stick his code into your MYTHEME_preprocess_node function and put your variable in a $variables value which your sample code doesnt do. He's showing you how to get a reference to the field_collection entity itself from its node, and then how to nab the field in question from the collection. You take your preprocess function, and inject his code into it. So you can write the following variable to have in your preprocess function. $vars['video_file_fid'] = $collection->field_video_file_col->value(); – tenken Apr 4 '13 at 15:35
  • @tenken thanks. Danny follow the tenken's comment. Just use entity_metadata_wrapper in MYTHEME_preprocess_node – milkovsky Apr 5 '13 at 10:57
  • I read the posts you linked too before I posted this question actually and could not really make heads or tales of it. Using code from above, I am getting WSODs.I am also seeing a message that the variable node_wrapper is not used anywhere. – Danny Englander Apr 6 '13 at 16:14
  • See example in my UPDATE 2. But please check field names("field_video_collection" and "field_video_file_col") P.S. You are right, there should have been $node_wrapper instead of $node. – milkovsky Apr 7 '13 at 20:07
-1

You may try this one. The first line gets the object, the second line the fid from it.

$field_video_col=current(current($vars["field_video_file_col"]));
$video_file_fid=$field_video_col['fid'];
  • current takes the argument by reference, so you need to put those in separate variables. – user49 Apr 6 '13 at 10:13
  • This did not work. I'm getting a message: Unused local variable 'video_file_fid'. The value of the variable is not used anywhere.A variable is considered unused in the following cases: * The value of the variable is not used anywhere or is overwritten immediately. * The reference stored in the variable is not used anywhere or is overwritten immediately – Danny Englander Apr 6 '13 at 16:10
  • Let's refactor like this then $tmp=current($vars["field_video_file_col"]); $field_video_col=current($tmp); $video_file_fid=$field_video_col['fid]; echo ($video_file_fid); – Mauro Sardu Apr 8 '13 at 8:35
  • The new code above did not work either. There's also a syntax error ['fid] should be ['fid'] but that's beside the point. – Danny Englander Apr 10 '13 at 14:57
-1

The code from dropbucket might be helpful to get and prepare all the field collection's data into an array:

$fieldname = 'field_lorem_slides';

// Get the items
$slides = array();
foreach($vars[$fieldname] as $delta => $item_id){
    $data = field_collection_item_load($item_id['value']);
    if ($data)){
                $l = isset($data->language) ? $data->language : LANGUAGE_NONE;
        $slides[$delta] = array();

                // Get the field collection's data prepared in an array
                // Alternatively, you could do $slides[$delta] = $data; in order to get the full objects in the array 
        $slides[$delta]['lorem_image'] = '<img src="'.file_create_url($data->field_image[$l][0]['uri']).'">';
        $slides[$delta]['lorem_data'] = isset($data->field_heading[$l]) ? $data->field_heading[$l][0]['safe_value'] : null;
    }
}

// Send the information to the template file
// $vars['slides'] = $slides;
  • Is this a template preprocess function? If so is it a preprocess page or node function? I'm not seeing any context so I think simply linking to a code snippet without explanation is not too useful. – Danny Englander Apr 4 '13 at 15:09
  • Yes it's meant for use in preprocess functions. By the way, when the team I work with was struggling with field collections the most productive approach was found to be installing the devel module and using dsm() to explore the variables in templates and preprocess functions. Field collections are a pig to theme, partly because (erroneously in my view) their field preprocess function gets called both for the field collection field and individual items within it. – Alfred Armstrong Apr 4 '13 at 15:14
  • So is it a _preprocess_node, _preprocess_page function or other? – Danny Englander Apr 4 '13 at 15:17
  • That would be a preprocess_node. – Alfred Armstrong Apr 4 '13 at 15:29
  • I tired this many different ways but could not get it work within the context of what I am doing, it just does not seem to be apples to apples. – Danny Englander Apr 6 '13 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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