2

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 }}

I thought it would be something easy like field+formatter:

{{ content.field_sd_video|videojs_formatter }}

But I can't find a simple way to achieve this.

2 Answers 2

5

You can do this in UI, UI and code mixed, code only and in html:

Display Suite

Install the module Display Suite and configure this in UI.

Extra Field

Use the hook hook_entity_extra_field() to get an extra field in Manage Display. You need hook_ENTITY_TYPE_view() to copy the data (in this case the file) into the new field.

Clone field in code

See How to display a field twice in a view mode?

The last has the least overhead, but is not customizable and you have to define the field settings yourself. If you configure the second field temporarily in UI and export the view mode to yaml this helps to find the settings you need in code.

Place link html in template

Or if you want a quick solution in the template and you only need a simple formatted file link you can get the file url from the node and put it in html markup:

<a href="{{ file_url(node.field_video.entity.uri.value) }}">Example Video</a>
4
  • Or you could preprocess node too, and create a new variable that themes the value of the filefield into a link, and output that in the twig template
    – Kevin
    Jan 12, 2017 at 14:25
  • Yes, @Kevin. I tried that. I'm trying to get the field value and change the formatter. But I'm not sure, how can I access to the field value. In the preprocess_node I've the $variables['node'] and I know exist the [videojs theme]8http://cgit.drupalcode.org/videojs/tree/README.md#n105) function.
    – ferriol
    Jan 12, 2017 at 14:50
  • @4k4 I would like to not have to implement a module, I believe that the 2nd and 3rd solution needs a custom module, isn't it ? I thought I could achieve this in my own bootstrap subtheme that I already have.
    – ferriol
    Jan 12, 2017 at 15:00
  • @ferriol, see my 4th example
    – 4uk4
    Jan 12, 2017 at 15:24
1

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

Thanks @4k4 your answer was usefull

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

1
  • That's a very helpful for others, providing the final solution. Btw. this is similar to drupal.stackexchange.com/questions/218144/…, only that you get the field object FileItemList from the node and in the linked question it is coming from the source field.
    – 4uk4
    Jan 13, 2017 at 12:30

Your Answer

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

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