0

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

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;
  • I updated my question. – Pankaj Yogi Jan 22 at 10:45
1

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

  • My image field is in paragraph. May I use same as node ? – Pankaj Yogi Jan 21 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 – Leigh Jan 22 at 9:18

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.