3

Reason: I need to create an Open graph meta tag og:image and pass the URL as content.

On THEME_preprocess_html(), I loaded the node using

$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
      $nid = $node->id();
      if($node->hasField('field_image')){
        $image_id = $node->field_image->target_id;
        $image = \Drupal::entityTypeManager()->getStorage('file')->load($image_id);
      }
}

I have loaded the node and I was able to get the field_image_entity_id as shown. I want to get the URL of the image field of entity referenced field.

The below code gives me NULL. How can I load the media entity and get this value?

$image = \Drupal::entityTypeManager()->getStorage('file')->load($entity_img_id);
5
  • Possible duplicate of Getting the image URL from a field_image on a node
    – GiorgosK
    Mar 13, 2019 at 9:32
  • @GiorgosK I want to get the url first then I can print it. I am trying to get this in a preprocess hook and will use the twig template to render it.
    – Regokonda
    Mar 13, 2019 at 19:31
  • I have tried the solution suggested by Berdir on that page but I am unable to load the url. $node->field_image->entity->getFileUri()
    – Regokonda
    Mar 13, 2019 at 19:32
  • What do you mean by its displaying the wrong file located in public directory, your question perhaps is missing some vital information. How are you getting the entity_img_id ?
    – GiorgosK
    Mar 13, 2019 at 20:45
  • Hi Giorgosk, I am getting the entity_image_id from $node->field_image->target_id; I am loading the entity using Media::load($field_image_id); this returns NULL, I used \Drupal::entityTypeManager()->getStorage('file')->load($entity_img_id); and this returns the wrong file.
    – Regokonda
    Mar 13, 2019 at 21:46

4 Answers 4

2

ok, here is what worked for me. After digging through stack overflow, found that I was missing the ImageStyle and this resulted in not rendering the url.

I added

use Drupal\media\Entity\Media;
use Drupal\image\Entity\ImageStyle;
use Drupal\media_entity\MediaInterface;
use Drupal\node\Entity\Node;

and then on preprocess function, I loaded the media using the target_id that I got form the $node->field_image->target_id;

Then, I loaded the ImageStyle using

$media = Media::load($image_id);
$media_url = ImageStyle::load('TYPE_OF_CROP')->buildUrl($media->image->entity->getFileUri());
$variables['media_url'] =  $media_url;

Hopefully this helps people.

2

file_create_url has been deprecated. Use \Drupal::service('file_url_generator') instead.

$image_uri = $node->field_my_media->entity->field_media_image->entity->getFileUri();

$image_url = \Drupal::service('file_url_generator')->generateAbsoluteString($image_uri);
1
  • 1
    This helped me with an image field in Drupal 10. I already had $node->field_my_media->entity but it had no getFileUri(). Only going deeper ...->entity->field_media_image->entity gave me the entity that I could call getFileUri() on. The first entity is Drupal\media\Entity\Media, the second entity is Drupal\file\Entity\File.
    – jox
    Dec 23, 2023 at 14:25
1

Just tested the following code and was able to get the image URL from field_image of the current node.

function THEMENAME_preprocess_html(&$variables) {
  //get the current node
  $node = \Drupal::routeMatch()->getParameter('node');
  if ($node instanceof \Drupal\node\NodeInterface) {
    if($node->hasField('field_image')){
      $image_url = file_create_url($node->field_image->entity->getFileUri());
    }
  }
}
6
  • Hi Giorgosk, Thanks for the response. I have tried this but it doesn't work. I am getting "Website is having an unexpected error" when I check logs, I don't see any errors logged. From what I notice, when the field_image tries to get to the entity($node->field_image->entity) it throws this error. I have used all the necessary classes on the theme file, including Node, Entity, Media, EntityManager
    – Regokonda
    Mar 13, 2019 at 21:38
  • The answer above is a working solution of what I see in your question. Are you trying to get the image field of the current node and pass to twig for displaying ? Please revise your question with all details about the image field and the node and fields setup and what you are trying to accomplish might help to understand better your problem.
    – GiorgosK
    Mar 13, 2019 at 22:03
  • Hi Giorgosk, I have updated the question again to best represent the issue.
    – Regokonda
    Mar 13, 2019 at 23:18
  • Still the title of your question is very different from what you are asking in the question. Please revise. Is $entity_img_id = 2581; known ? how did you get it ?
    – GiorgosK
    Mar 14, 2019 at 5:50
  • As mentioned I got the image id from the $node->field_image->target_id. Thanks for the input
    – Regokonda
    Mar 14, 2019 at 6:00
0

In more recent version of Drupal 8, you can use:


$image_field = $node->get('field_image');
if ($image_field->isEmpty())
  return;
$media_entity = $image_field->referencedEntities()[0];
$uri = $media_entity->get('field_media_image')->entity->uri->value;
$variables['image_url'] = file_create_url($uri);

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.