0

I have a simple REST export View. The View format is set to "Show: Entity".

The content type I am exporting contains a standard image field. My image field settings is set to "Enable Alt field" and "Alt field is required".

Therefore all my nodes that have an image, have an image alternate text (Alt).

In the JSON that my view outputs, there is nowhere that I can find the value of the image alternate text.

For each image, I get the filesize, the filename, the filemime, the uri, etc. etc. but no alt text.

Any idea why? And how could I retrieve it? (other than by replacing the image alt text by a separate field...)

1

Views Format: Fields

I don't think there is a special separate field in Views UI for the Alt field. Alt text is part of the Image field data, stored with the content entity (not the file).

So what you do is add another Image field to your Views, but configure it this way:

  1. Rewrite results
  2. Override the output of this field with custom text
  3. Check Replacement patterns below to find your Alt token
  4. Enter the replacement pattern into the above Text field {{ field_image__alt }}

Now you have the image Alt text showing as the field output instead of the actual image.


Edit: The above can only help if you're using Fields under your Format settings in Views.

Views Format: Content/Entity

When showing full entities in Views you need to use Content Type/Entity View modes (not to be confused with anything Views related). On your CT add a new View mode on the Manage display page, or adjust the existing one you're using in your Views Format settings.

Custom Image field formatter

This is where you should be able to adjust your Image field Formatter, but unfortunately the Image field doesn't seem to come with the "Alt" text formatter.

Other than creating the custom Formatter yourself you can check out this Issues and module:

  • That would certainly work, except that I mentioned I am not using fields (Show: Entity) – MrUpsidown May 18 at 8:01
  • 1
    Yes, you did, and I did read it, but it evaporated by the time I got to answering it lol I'll update my answer. – prkos May 19 at 20:14
  • Ok... thanks for that. I think for my use case, as I am only using Drupal as "headless" and the REST export Views, it might actually be easier to add a separate text field to hold that ALT information. – MrUpsidown May 21 at 10:48

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.