I need to check if an image media field is housed in a paragraph to remove the alt attribute of that attribute. The same image field could be referenced anywhere. I could remove the alt field it within preprocess_image by checking if the value is set to a particular value.

However I need a more robust method by checking if the field is housed inside a paragraph/belongs to a paragraph. Maybe we can pass a variable from the parent template to the image template variables?

  • 1
    Media entities are reuseable, self-contained, independent entities, you can not pass a variable in there. Instead, try to maniplate the parent, e.g. preprocess the paragraph and remove the alt attribute from the field's render array.
    – Hudri
    Oct 28, 2022 at 8:08
  • I thought of doing that. Do you have examples of preprocess paragraph?
    – Binny
    Oct 28, 2022 at 9:32
  • Put a normal image field in the paragaph type. Then the alt property is stored in the paragraph and you can change it in UI or programmatically for the specific paragraph.
    – 4uk4
    Oct 28, 2022 at 11:31


Your Answer

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

Browse other questions tagged or ask your own question.