I want to use a single webform to send download links with specific media files to visitors that fill out that form. For example on an URL like this:


The media parameter will be the ID of an media entity type like "Download". Now I want to send the mail with the name of the media entity and a link to the file of that entity.

How could I get this done? Mail handler and a custom Twig template is what I think about, but how do I get the data of the media entity? Any ideas? Thanks for every helpful comment ;)

1 Answer 1

$media_id = \Drupal::request()->get('media'); // get value of media query param

$media = Drupal\media\Entity\Media::load($media_id); // load the Media entity

$media_name = $media->getName(); // get the media item name

$media_field = $media->get('field_media_image')->first()->getValue(); // get media field

$file_id = $media_field['target_id']; // get the File ID

$file =  Drupal\file\Entity\File::load($file_id); // load File entity

$image_url = $file->url(); // get image url

You could take this shortcut instead of writing the last 4 lines.

$image_url = $media->get('field_media_image')->entity->url();

Your Answer

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

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