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".
1 Answer
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. Commented Oct 26, 2017 at 23:32