🇬🇧
Drupal Documentation and Guides
Guides
English
English
  • Getting Started with Drupal
  • Installing Drupal
  • Drush
  • Theme & Module Development Concepts
  • Modules
  • Themes
  • FrontEnd
    • Javascript
  • Backend
    • Helpful Drupal Classes and Methods
    • Entities
    • Nodes
    • Taxonomies
    • Hooks
    • Twig
    • Libraries
    • Queries
    • Forms
    • Files & Images
    • Helpful functions and solutions
  • Guides
    • Custom Fields
      • Entity Reference and integer
      • Two Text Fields
      • Textfields, formatted text and numbers
      • Links and media elements
Powered by GitBook
On this page
  1. Backend

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);
}
PreviousFormsNextHelpful functions and solutions

Last updated 2 years ago