mmikkel / Retcon-Craft

A collection of powerful Twig filters for modifying HTML
MIT License
79 stars 9 forks source link

[4.x] retconAutoAlt doesn't use asset's title #71

Closed anita-chouhan closed 1 month ago

anita-chouhan commented 1 month ago

The retconAutoAlt renders the asset's filename in place of the title (as mentioned in the doc).

Expected {{ html |retconAutoAlt }} Asset title as alt tag value if no alt value is supplied by the author for the rich content field image.

Outcome {{ html |retconAutoAlt }} Asset filename as alt tag value if no alt value is supplied.

Plugin version 2.8.0

Craft CMS version 4.9.7

mmikkel commented 1 month ago

I'm not able to reproduce that.

Where does the html variable come from and what does it contain? If you do a {{ dump(html) }} in your template, what does the output look like (a screen dump is fine, if you can share)?

anita-chouhan commented 1 month ago

The variable contains the rich text content from CKEditor. Here is the output of {{ dump(html) }} and {{ dump(html|retconAutoAlt) }}.

Screenshot 2024-10-14 at 10 33 19 AM Screenshot 2024-10-14 at 10 25 26 AM

The asset title is Colorful bird and file name is bird-thumbnail.jpeg. I was expecting the alt to show Colorful bird and not bird-thumbnail.

Screenshot 2024-10-14 at 10 27 00 AM
mmikkel commented 1 month ago

Thanks for the extra info!

The reason why retconAutoAlt isn’t able to output the asset’s title is because your CKEditor field’s output has already been rendered to a string (or rather, a Twig\Markup object) - hence the reference tags found in raw CKEditor output have been lost. For the autoAlt feature, Retcon relies on those tags for querying for the original asset, falling back to using the file name if it can’t be found.

Most likely, this occurs because the CKEditor output has been passed through another Twig filter (or some other operation that renders its raw content down to a Twig markup object), before the retconAutoAlt operation.

To ensure that the retconAutoAlt filter works as intended, make sure that this filter is the first operation on the CKEditor output.

anita-chouhan commented 1 month ago

Okay, thank you for looking into it. I'll check for the other filters.

itsmattsoria commented 1 month ago

I can recreate this same behavior. I've ensured that the very first filter applied is retconAutoAlt, and even tested turning Purify HTML off for the filter, and still no matter how I set the three options, or if I just leave them as default, I'm getting the filename output as the alt text value. I'm on Craft 5.4.7.1 and Retcon 3.2.0. I also had some other Retcon filters I was applying and I tested removing all of them and still got the same result.

mmikkel commented 1 month ago

@itsmattsoria Can you share the relevant Twig code where this is occurring? Are you using CKEditor or Redactor?

itsmattsoria commented 1 month ago

@mmikkel I'm sorry, I'm using CKEditor 4.2.0. Here's my twig code:

{{ entry.longFormContent|retconAutoAlt(null, false, false) }}

Where longFormContent is a CKEditor field.

mmikkel commented 1 month ago

Thanks @itsmattsoria, I'm able to reproduce. Seems like it's isolated to Craft 5/Retcon 3.x. I'll get a fix out ASAP.

itsmattsoria commented 1 month ago

@mmikkel Amazing—thank you!

mmikkel commented 1 month ago

This turned out to be a regression introduced in https://github.com/mmikkel/Retcon-Craft/commit/17a58efd73c248e9b7304771c7a9b95d339466ee (only affects the Craft 5 version of Retcon). I'll get a release out w/ the fix as soon as possible, but there are a couple of other things I'm hoping to get to. If you need it straight it away, change your Composer dependency for Retcon in composer.json to

"mmikkel/retcon": "dev-master#dcd459ac22f9fa54b9b149e9057d4ec83c98c6c0 as 3.2.0"

...then run composer update.

itsmattsoria commented 1 month ago

@mmikkel awesome, thanks!

mmikkel commented 1 month ago

@itsmattsoria Retcon v. 3.2.1 is out now with that fix for Craft 5.

itsmattsoria commented 1 month ago

@mmikkel amazing, thank you!