I'm trying to configure my site (which was upgraded using Migrate Drupal from 7 to 8) to have images inserted into the body.

I have an 'Image' field added to my content type.
I configured the Insert form display settings to allow Inserting the "Original" image.
When I insert an image (which has a required Alt text field), the resulting markup doesn't contain the alt text.

In fact, I overrode the template entirely and printed {{ alt }}, and it was empty. I then overrode the theme preprocess hook to check what data was available, and I could see that the File entity was loaded and available, but if I tried doing a $vars['file']->get('alt'); on it, it would not load any alt text and my site had the WSOD.

I'm kind of scratching my head... the field is a core image field:

Image field example

But when I try to insert it, there is just the src (which is correct), width/height (though they are not taking into account my 'max insert width' setting as noted in #3144269: Maximum image insert width setting not applying when inserting original image), and all the data-src attributes with the file UUID and such. No alt text.

Comments

geerlingguy created an issue. See original summary.

jascote’s picture

IDK if this is your issue, but if you have "Apply caption to images" on for the insert widget settings on that field your alt/title values get overwritten by this code in the template:

{% if insert_settings.caption %}
  {% set attach = attach|merge({attributes: {'data-caption': ['title']}}) %}
{% endif %}
geerlingguy’s picture

StatusFileSize
new144.57 KB

@jascote - In my case, I don't have that enabled, but thanks for suggesting:

Alternative captions not enabled

When checking in twig itself, it seems like the alt text variable doesn't contain anything at all, like this module's not populating that value.

snater’s picture

I'm unable to reproduce the issue setting up a fresh Drupal 9.1.5 instance with just the Insert module installed. Content type has a basic image field with Insert enabled to insert original image and alt field required. Inserting the image into the body and saving the node results in the alt attribute being rendered in the HTML on the image tag.

It's been a while since I have dealt with the code, but the alt attribute should be applied using the CKE image widget as that allows editing image attributes per the image button in the editor toolbar. There is no alt variable passed to the insert-image.html.twig template, so I would not expect {{ alt }} to output anything—that is expected behaviour.

If the alt attribute is not rendered in the HTML output, please provide the exact Drupal core version you are using and the exact steps to reproduce. It would be awesome if you could also check if you would have the issue on a fresh clean Drupal installation with just the Insert module installed to check if there might be some conflict with one of your other modules installed.

geerlingguy’s picture

@Snater - I'm not using CKEditor, just a plain body field textarea with the Markdown input filter. Is it possible this module doesn't work correctly if you're not using CKEditor?

snater’s picture

I'm sorry for like taking months on this to get back… finally having a day off. I could see this issue might be outdated by now, but was still interested looking at this again, though I don't expect an answer as it's months ago.

I cannot reproduce on a plain textarea either. I even uninstalled the CKEditor module. Rolled things up again stepping through the code… there could be multiple places where some Insert module config might just be mixed up. After all, it would be great to see what is actually inserted in the textarea as that might give some hint on what might be missing. The data-insert-attach attribute being present is vital as that defines where to pick up the alt text from—the values of data-insert-attach are internally mapped to jQuery selectors.

Generally, the alt text is purely handled by JavaScript, which is retrieving the alt text value per jQuery from the alt text input. Potentially, there could be something going wrong in JavaScript causing a JS error to appear in the browser's dev console when inserting the image.

It would be nice to step into the code with the dev tools debugger and see if the alt input is actually picked up by the code as per https://git.drupalcode.org/project/insert/-/blob/8.x-2.x/js/ImageHandler.... this._$altField should be assigned a reference to the alt input. If not, need to check why the selector 'input[name$="[alt]"], textarea[name$="[alt]"]' is not picking up the alt input.

There are image styles which prevent the alt text from being inserted (link and icon_link), but since original is being used, that should ne be an issue.

geerlingguy’s picture

I may take another crack soon—I finally upgraded my site to Drupal 9 and with the latest version of Insert I can now see the max image width attribute is working again (happy about that!). I can confirm I'm still seeing this behavior right now, but may also try taking a look on the theme layer and see if something in the Gin admin theme could be interfering.

snater’s picture

Status: Active » Closed (cannot reproduce)

Feel free to reopen if still relevant.