8

How do I easiest get an image (its path) from the article content type included in Drupal 8?

What I have is an entity. Using $image = $node->fields['field_image'] gives me a hell of a structure, though.

Is there no easier way to get the image path? Also I cannot find the URL in this, what I get is some sort of target_id (the file ID?).

  • What does get_class ($node->fields['field_image']) give you? – Clive Dec 10 '14 at 11:10
  • Drupal\file\Plugin\Field\FieldType\FileFieldItemList is what it gives me, this only seem to contain properties of what is allowed to upload. – Undrium Dec 10 '14 at 12:52
  • If you iterate the list what types are its members? I haven't checked, but I'd hope for some reasonably strong typing of the field. If all else fails you can use file_load() and one of the methods available on Drupal\file\FileInterface – Clive Dec 10 '14 at 12:54
  • Drupal\image\Plugin\Field\FieldType\ImageItem is what I get, not sure how to properly handling that, though. – Undrium Dec 10 '14 at 13:12
  • file_load is a last resort, but it's depricated and feels so wrong. – Undrium Dec 10 '14 at 13:12
17

Didn't see this before.

The shortest way to get the referenced entity for a single-value field is this:

$node->field_image->entity->url()

You can also specify the delta explicitly:

$node->field_image[0]->entity->url()

This works with a lot of ArrayAccess and __get() magic.

See also the great cheat sheet at http://wizzlern.nl/drupal/drupal-8-entity-cheat-sheet. and https://www.drupal.org/node/1795854 and the other documentation pages there for more information (still very much work in progress).

  • Nice! I was wondering how we can get the image alt. I tried $node->field_image[0]->entity->alt and $node->field_image[0]->entity->image->alt; which I found in the documentation you mentioned but it does not work with the node entity I think. – Hedeshy Feb 1 '18 at 10:10
  • 1
    the alt/title is not on the referenced file entity but the field item itself. Just $node->field_image[0]->alt should work fine. – Berdir Feb 1 '18 at 10:54
3

What ended up working for me in D8 is:

$imageUrl = $node->get('field_image')->entity->uri->value;

Using kint($node->get('field_image')->entity) and looking through the array was very helpful

enter image description here

Then in my twig file I used:

<img class="top-article-image" src="{{ file_url(imageUrl) }}" />
1

So my solution was to use the method "referencedEntities()" found in the nodeobject. This gave me an array of File-objects which I could iterate through and use the method "url()" from the file objects to retrieve a valid path.

0

If your node has more than one image you can use this to get a array of the image urls (helpful for making slideshow modules among alot of other useful things)

$allImagesFound = false;
$imageUrls = array();
$numberOfImages = 0;
while(!$allImagesFound)
{
    //make sure we are not calling a null value, if so then we have found all images
    if($node->get('your_image_field_name')[$numberOfImages] != null) {
        $imageUrls[] = $node->get('your_image_field_name')[$numberOfImages]->entity->url();
        $numberOfImages++;
    } else {
        $allImagesFound = true;
        break;
    }
}

The imageUrls array will hold all the urls for any images associated with the nodes field. Hope this helps.

Your Answer

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

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