1

I'm experiencing an inconsistent behaviour in Drupal 7 on the function file_create_url(). I have one field collection with 4 fields in it. On the first field the function provides the correct file url. For the other 3 fields it displays only first part of url. How can I get rid of this?

Here is my code:

<video controls="controls"  autoplay="autoplay" poster="<?php print file_create_url($content["field_poster"][0]["#item"]["uri"]); ?>" style="width:100%"   >
  <source src="<?php print file_create_url($content["field_mp4"][0]["#item"]["uri"]); ?>" type="video/mp4" />
  <source src="<?php print file_create_url($content["field_webm"][0]["#item"]["uri"]); ?>" type="video/webm" />
  <source src="<?php print file_create_url($content["field_ogg_theora"][0]["#item"]["uri"]); ?>" type="video/ogg" />
</video>

And here is the result: field_poster is fine but the others fail; enter image description here

<?php print_r($content["field_poster"][0]); print_r($content["field_mp4"][0]); ?>

leads to...

            Array
(
[#theme] => image_formatter
[#item] => Array
    (
        [fid] => 288
        [uid] => 1
        [filename] => Transgourmet_Waldputen_MASTER.jpg
        [uri] => public://Transgourmet_Waldputen_MASTER.jpg
        [filemime] => image/jpeg
        [filesize] => 65241
        [status] => 1
        [timestamp] => 1440456242
        [type] => image
        [rdf_mapping] => Array
            (
            )

        [image_dimensions] => Array
            (
                [width] => 1008
                [height] => 573
            )

        [alt] => 
        [title] => 
        [width] => 1008
        [height] => 573
    )

[#image_style] => 
[#path] => 
)
Array
(
[#theme] => file_link
[#file] => stdClass Object
    (
        [fid] => 289
        [uid] => 1
        [filename] => Transgourmet_Waldputen_MASTER.m4v
        [uri] => public://Transgourmet_Waldputen_MASTER.m4v
        [filemime] => video/x-m4v
        [filesize] => 34256348
        [status] => 1
        [timestamp] => 1440457177
        [type] => video
        [rdf_mapping] => Array
            (
            )

        [display] => 1
        [description] => 
    )

)

Is it possible that is has to do with the mime-types?

Thanks in advance.

  • If you reorder them will the first path in the collection always be correct, no matter the field? Have you tried to print out your field values outside of the collection? – othermachines Aug 25 '15 at 2:50
  • Thanks for your answer. I reordered them bit nothing interesting happened: ´field_poster` is correct, field_mp4 fails....moreover when i ´print_r($content['field_mp4'][0])` ist display an object with the right file infos in it. When I try access ist it fails.... – radscheit Aug 25 '15 at 7:03
  • Yeah, that was kind of a silly question now that I think about it, since this is manual, not dynamic, output. What does the print_r() output look like for $content['field_poster'] and $content['field_mp4']? For sake of comparison can you include it in your post? – othermachines Aug 25 '15 at 7:29
  • Thanks. I completed my question in the way you suggested. Any idea? :) – radscheit Aug 25 '15 at 7:37
  • When I try to access the object via ["#file"] instead of ["#item"] the whole site fails... – radscheit Aug 25 '15 at 7:45
1

For each of the video files, this (for example):

$content["field_mp4"][0]['#item']['uri']

should be this:

$content["field_mp4"][0]['#file']->uri

Change #item to #file and change the syntax to reference an object instead of an array.

  • Unfortunately, I'm not allowed to upvote right at the moment :) But I labeled it as solution. – radscheit Aug 25 '15 at 7:53
  • No worries. :-) – othermachines Aug 25 '15 at 7:54
  • Just to get a learning out of it: The one is an array and the other is an object? Is that the reason for different syntax? – radscheit Aug 25 '15 at 8:02
  • That's exactly right. This is where the print_r() (or better, dpm() if you use the Devel module) comes in handy, since you can see things like that in the output. – othermachines Aug 25 '15 at 8:06

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.