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:

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.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | Screen Shot 2020-09-11 at 3.42.29 PM.png | 144.57 KB | geerlingguy |
| Screen Shot 2020-05-31 at 10.17.18 PM.png | 180.04 KB | geerlingguy |
Comments
Comment #2
geerlingguy commentedPossibly related?
Comment #3
jascote commentedIDK 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:
Comment #4
geerlingguy commented@jascote - In my case, I don't have that enabled, but thanks for suggesting:
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.
Comment #5
snater commentedI'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
altfield required. Inserting the image into the body and saving the node results in thealtattribute being rendered in the HTML on the image tag.It's been a while since I have dealt with the code, but the
altattribute should be applied using the CKE image widget as that allows editing image attributes per the image button in the editor toolbar. There is noaltvariable passed to theinsert-image.html.twigtemplate, so I would not expect{{ alt }}to output anything—that is expected behaviour.If the
altattribute 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.Comment #6
geerlingguy commented@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?
Comment #7
snater commentedI'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-attachattribute being present is vital as that defines where to pick up the alt text from—the values ofdata-insert-attachare 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._$altFieldshould 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 (
linkandicon_link), but sinceoriginalis being used, that should ne be an issue.Comment #8
geerlingguy commentedI 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.
Comment #9
snater commentedFeel free to reopen if still relevant.