Files & Images

Example of how we work with files and images in Drupal 8/9.

Get the url of an image

In this example, we get the url of an image that is added via a MEDIA field called "field_image".

use Drupal\file\Entity\File;

$mid = $node->field_image->target_id;
if (!empty($mid)) {
   // Crear enlace de la imagen
   $file = File::load($mid);
   $url = $file->getFileUri();
   $m_url = file_create_url($url);
}

Last updated