1

When an entry includes a reference to an asset, like an image, that information is tracked and displayed. (The included screenshot shows where that reference would be, although this particular entry does not have any related content.)

What I'm wondering is, is there a way to track asset references that occur within a Redactor field? Ideally, I could output this in the asset list view. Purpose: to clean up our asset library. We have tons of duplicates, but right now, there is no way to know if a given asset is referred to in a Redactor field.

Note: I use the Preparse plugin a lot, maybe there's a way to do something like:

{% if element.myField contains '#asset' %}...{% endif %}

and have a column that indicates if there exist one or more asset references?

1

2 Answers 2

0
+50

Since you only have raw HTML in the redactor field and there is no real relationship between the asset in the craft and the img url, the only way to do this is to parse your readctor field (or maybe even this?), check the img tags, take the "src" part and check in the assets table (the last part of the path in the src).

...<img src="http://mysite.com/assetFolder/subfolder/**file_name.jpg**">

And then find it like this:

\craft\elements\Asset::find()->filename($parsedImgSrcValue);
2
  • Would it be possible to do this using the Preparse plug-in? Does Twig have a way to determine if the contents of the field contain a matching string? Something like regex? Per my example: if X contains "{string}"...then... I don't have the PHP skills to use the method you describe.
    – 4midori
    Commented Mar 14, 2023 at 23:41
  • 1
    You can do {% if 'string' in block.fieldHandle ...' with twig, the in operator should work for checking the redactor contents. twig.symfony.com/doc/3.x/templates.html#containment-operator
    – cherrykoda
    Commented Mar 15, 2023 at 1:11
0

If your end goal is simply to have a dynamic index/list of all entries where that field contains that string, you can use the new filtering options in the entries index list here:

enter image description here

2
  • Hmm, actually I've just noticed that this filter UI doesn't seem to include Redactor fields for some reason. Weird. Commented Mar 16, 2023 at 13:37
  • Alas, this is Craft 3... Good idea, however.
    – 4midori
    Commented Mar 16, 2023 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.