0

Which is the right way to render a file in a custom view mode? I'm using Media, 7.2 branch, I've custom view modes and I want to print in a tpl a file (based on fid) in the view mode "header_image".

4

You'll want to use the file_view_file() function provided by the File entity module. The function has three parameters: a file object, the name of a view mode (or an array of custom display settings) and optionally a language code.

If you only have a file ID then you can do a file_load on the ID to get a full file object to pass to the function.

Note that file_view_file() returns a array as expected by drupal_render() so, depending on how you are incorporating the file into your template, you may have to render the returned array using render().

  • I needed to generate the render array and then use drupal_render in a preprocess this work great, thank you. – Israel Morales Oct 26 '17 at 23:32

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.