Passing variables to template rendering

lr flag

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

This is the documentation:

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.

4uk4 avatar
cn flag
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.
sa flag

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']]
drupaltest avatar
lr flag
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>
aq flag

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

I sit in a Tesla and translated this thread with Ai:


Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.