5

I want to print alt or title field image file in custom module Drupal 8. I load file entity use Drupal\file\Entity\File; I checked core/modules/file/src/Entity/File.phpall public function but not get any function which return title or alt text.

$graph_data = Paragraph::load($graph['target_id']); $output .= '<div class="swiper-slide">'; $output .= '<div class="slide-inner">'; foreach ($graph_data->field_slider_image as $item) { $title_img = "Need image title here"; if ($item->entity) { $slide_uri = $item->entity->getFileUri(); $url = ImageStyle::load('orignal')->buildUrl($slide_uri); $output .= '<div class="col-md-6"><img src="'.$url.'" class="img-responsive"><h2>'.$title_img.'</h2></div>'; } } 

Please anyone give idea ?

Thankyou

2 Answers 2

5

You can load the node and then fetch the paragraph targetid . Later you can load paragraph targetid and then get all attributes of the image.

use Drupal\paragraphs\Entity\Paragraph; use Drupal\node\Entity\Node; $node = Node::load($nid); $paragraph_field = $node->get("field_paragraph")->getValue(); $paragraph = Paragraph::load($paragraph_field[0]['target_id']); $image_alt = $paragraph->my_image_field->alt; $image_title= $paragraph->my_image_field->title; 
0
5

That information isn't stored against the file entity, its on the image field attached to your source entity (e.g. your node)

So you could do something like:

$node->my_image_field->alt $node->my_image_field->title 

and that would give you the alt/title

2
  • My image field is in paragraph. May I use same as node ? Commented Jan 21, 2019 at 17:41
  • Yeah you'd just load the paragraph entity and do the same as above: $paragraph->->my_image_field->alt nodes/paragraphs and most other things are all just entities Commented Jan 22, 2019 at 9:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.