I created a custom bundle (content type) and I've created fields

One of those field is a file type field, is a video file, stored in the private storage. I installed the videojs module to allow to watch the video.

I need to show this field twice in the node page. One in the generic file formatter that allows the user to download the file, and I also need to play the video. I decided to set up as generic file formatter and customize the twig template to show again with the video player.

I achieve to show twice with the settings formatter (generic file) with this code in the node--mybundle--full.html.twig template

{{ content.field_sd_video }}

But I don't know to show in the video player.

I saw the videojs theme function

But I don't know how to get the field value in the twig template or in template_preprocess_node function and format it as videojs player

Comments

ferriol created an issue. See original summary.

ferriol’s picture

Assigned: Unassigned » ferriol
Status: Active » Fixed

I finally found the solution, I put this code in preprocess node function. It has to be easy, but not easy to know how ;)

    function mytheme_preprocess_node(&$variables) {
    
         $variables['video_caption'] = $variables['node']->get('field_video')
             ->view(array(
                'label' => 'hidden',
                'type' => 'videojs_player_list'
            ));
    }

and I only has to add this line in twig file:

{{ video_caption }}

in my case node--mybundle--full.html.twig template

For those arrive here looking for something similar
I was inspired by Twig Recipes on page 41

It was usefull for me Twig debugging. Playing with kint and node variable and the classes used to wrap the information. Then I found that the field comes with FileFieldItemList and then I found the view method that uses EntityViewBuilderInterface

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.