0

I am using the Video.js (HTML5 Video Player) module and I would like to add an image for the poster by loading the URL of another image field in the same node (without modifying the module itself).

How can I preprocess that URL to get it inside the module template?

I tried to get the URL variable as {{ poster }} into the template file videojs.html.twig, by adding following preprocess function in the theme-file: function templatename_preprocess_videojs(&$variables, $hook) { if ($variables['element']['#field_name'] == 'field_image_poster') { $variables['poster'] = $variables['element']['#object']->getURL(); } }

PS: Thank you for the suggested solution. This would work if I would have expressed myself correctly. But it's not the field level were I do not get the variable, its one level deeper (the video template beneath the file field level).

  • Welcome to Drupal Answers! This is a rather broad question as it is asking what code to use without showing any code. The code is at least necessary to avoid a reply like I already tried that code and it doesn't work given to an answer showing the code to use. If you still have to write code to achieve what you described, you are asking the question too early. – kiamlaluno Oct 16 '19 at 10:42
  • I totally understand your review. I am a little bit wooried about my lack of understanding of preprocess functions in general. Nevertheless I added the code I tried to achieve my goal. – Benjamin Oct 16 '19 at 11:38
  • 2
    Possible duplicate of Get Image URL in Twig - element[#object] is the parent node, not the image field. – Hudri Oct 16 '19 at 12:15
  • Thank you for the suggested solution that would work if I would have expressed myself correctly. But it's not the field level were I do not get the variable, its one level deeper (the video template beneath the file field). – Benjamin Oct 16 '19 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.