1

I created a custom content type that its name is News and has an image field with news_image machanic name and I added an alternative text to it from administration page, now I want to print that alternative text when the node content type twig template rendered.
what I want to achieve finally is schematically something like below:

<img src="#" title="#" alt="Alternative text">

It seems this problem can be traced back to custom content type only because alternative text can be printed without any problem in default content type like article.

  • It already does this by default. ImageFormatter (wrong link sorry). – mradcliffe Aug 29 '16 at 19:49
  • I tried with simplytest.me and this is what I did - imgur.com/GinCNnO (currently at rr9hd.ply.st, but that site goes away). – mradcliffe Aug 29 '16 at 19:56
  • Maybe there is something else custom with the content type, node template, entity/node render, or field rendering. – mradcliffe Aug 29 '16 at 19:57
  • what is your method to render your field in node twig file? did you render with {{ content.news_image}} ? – Yusef Sep 24 '16 at 21:22
  • No, I use this method <img src="{{ file_url(node.field_news_image.entity.fileuri) }}" > – Mojtaba Reyhani Sep 24 '16 at 21:37
1

you can get "alt" property with node.FIELD_MACHINE_NAME.alt (and for image title as the same ), So your answer is

<img src="{{ file_url(node.field_news_image.entity.fileuri) }}" alt="{{ node.field_news_image.alt }}" > 
|improve this answer|||||
  • @MojtabaReyhani you're welcome :) – Yusef Sep 25 '16 at 6:29

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.