Score:-1

Passing variables to template rendering

lr flag

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.

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.
Score:0
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'

Try:

'#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>
Score:0
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">
</video>

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:

mangohost

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.