I want to render "file-video.html.twig" template.

This is the documentation: https://api.drupal.org/api/drupal/core%21modules%21file%21templates%21file-video.html.twig/9.3.x

This is my code:

 return [
                        '#theme' => 'file_video',
                        '#attributes' => ['TEST ATTR'],
                        '#files' => [
                                'file' => $file,
                                'source_attributes' => [

Where $file is Drupal\file\Entity\File. I tried 'source_attributes' => ['src' => $url], ... But everything in this array throws an error.

The problem seems that those are not variables passed to the correct template I want. The #attributes TEST ATTR goes into block div id instead of the template.

The correct template IS rendered, but nothing is passed to is. How should I format the return array in order to pass the variables into the template I want? I searched for couple hours and did not find any documentation whatsoever about this.

  • 1
    Please add to the question how the file media field and your block are connected. If the field is part of a custom block type you need to hook in the entity build process and not the block build.
    – 4uk4
    Feb 10, 2023 at 10:46

2 Answers 2


I think you should check: core/modules/file/src/Plugin/Field/FieldFormatter/FileMediaFormatterBase.php where 'source_attributes' is a Drupal\Core\Template\Attribute object.

 * @return array
 *   Numerically indexed array, which again contains an associative array with
 *  the following key/values:
 *     - file => \Drupal\file\Entity\File
 *     - source_attributes => \Drupal\Core\Template\Attribute

I forgot about your '#attributes'


'#attributes' => ['TEST' => ['ATTR']]
  • I think I have problem in how to pass the array from Block build() function. It seems that even when I am calling the template "file_video", the "attributes" parameter is "eaten" somewhere else, before rendering the correct template. It is not passed to the "file-video.html.twig", but to the block template, so the final HTML looks like <div "TEST"="ATTR"> .... FILE-VIDEO.HTML.TWIG.... </div>
    – drupaltest
    Feb 10, 2023 at 8:43

I've just tested this out in a block and can 100% confirm it works:

use Drupal\Core\Template\Attribute;
use Drupal\file\Entity\File;

$file = File::load(60);
$attributes = new Attribute();
$attributes->setAttribute('src', '/sites/default/files/MYVIDPATHHERE.mp4');
$build['vid'] = [
  '#theme' => 'file_video',
  '#attributes' => ['asda' => 'aa'],
  '#files' => [
      'file' => $file,
      'source_attributes' => $attributes,
  '#cache' => ['tags' => []],

Which results in HTMl of:

<video asda="aa">
  <source src="/sites/default/files/MYVIDPATHHERE.mp4">

Obviously you could retreive the video src from $file, rather than just hard coding it like I have.

If the above code isn't working for you then you've got something else going on interferring with the rendering

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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