3

Sorry for being a little bit impudent, bit i would need someone to build an array into a code snippet. As I know nearly nothing about php coding, I managed to adept some code I found to get the urls of video files stored in field collection fields (one is for the .mp4 movie, otherone for the .webm version). So far, the code works well, but - here comes the problem - there is only the first value of each field collection field displayed. So, I Think i would need an array to regenerate the div container containing the tag with the source paths for each value of the field collection, not only for the first one. I anybody can help me, I would be very, very thankful.... i am working on this since more than 6 hours now...

here is my code:

// get node object
$node = node_load($nid);

// get field collections from node 
$field_html_5_video = field_get_items('node', $node, 'field_html_5_video');

// get all fields values if collection exists
if (!empty($field_html_5_video)){
    $field = field_view_value('node',$node, 'field_html_5_video', $field_html_5_video[0]);  
    foreach ($field['entity']['field_collection_item'] as $id => $field_collection){

        // load the field collection item entity
        $field_collection_item = field_collection_item_load($id);
        // wrap the entity and make it easier to get the values of fields
        $field_wrapper = entity_metadata_wrapper('field_collection_item', $field_collection_item);

        // all values from the field collection
        $field_video_mp4        = $field_wrapper->field_video_mp4->value();
        $field_video_webm        = $field_wrapper->field_video_webm->value(); // an array of image data
        $field_video_description  = $field_wrapper->field_video_description->value(); 

        // getting the video urls
        $video_url_mp4          = $field_video_mp4['uri'] ? file_create_url($field_video_mp4['uri']) : '';
        $video_url_webm          = $field_video_webm['uri'] ? file_create_url($field_video_webm['uri']) : '';


        print '<div class="singleswipe">';
        print '<span>';
        print '<span class="inner">';
        print '<video width="100%" controls>';
        print '<source src="' .$video_url_mp4. '" type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\'>';
        print '<source src="' .$video_url_webm. '" type=\'video/webm; codecs="vp8, vorbis"\'>';

        print '</video>';
        print '</span></span>';
        print '<span class="datainfo">' .$field_video_description. '</span>';
        print '</div>';
    }
}
3
  • what is happening exactly? Try instead of printing your html start a var outside your foreach and then $template .= '<html>'; adding to it, then print it out after your foreach has finished looping. Try not using the entity_metadata_wrapper, as it seems like an extra layer of stuff to deal with. Keep trying also... You will figure it out. Code looks good, just needs some adjustment. Oct 4, 2013 at 22:30
  • Thank you for your reply, but - I am sorry - i don't understand what you mean. The issue is, that this code prints just one div container/video tag with the first url values of the field collection. Because of my lack in php knowledge I am not able to loop it or insert some sort of array. To be honest: I don't know how to start a "var" outside my foreach. By "var", do you mean a variable or the deprecated (already googled it!) php keyword "var"?
    – julmoel
    Oct 5, 2013 at 7:33
  • sorry make an array outside the foreach. $newarray = array(); Oct 6, 2013 at 19:29

1 Answer 1

0

As per @Andrew comment, instead what you have try printing your HTML start a var outside your foreach and then:

$template .= '<html>';

adding to it, then print it out after your foreach has finished looping.

In addition make an array outside the foreach, e.g.:

$newarray = array();

Try not using the entity_metadata_wrapper, as it seems like an extra layer of stuff to deal with.

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