Hello,

Is there a way to add a field base processor? Something that would allow you to say add a image uri field to the search api, and then specific that that field should use a set image style, or maybe do something else like alter the value but based on configuration for that individual field vs the entire entity index (so you could say have two images with different image styles)?

CommentFileSizeAuthor
#46 interdiff-2962569-43-46.txt9.82 KBdmytro-aragorn
#46 search-api-image-styles-urls-for-media-2962569-46.patch9.93 KBdmytro-aragorn
#43 interdiff-2962569-42-43.txt810 bytesdmytro-aragorn
#43 search-api-image-styles-urls-for-media-2962569-43.patch9.31 KBdmytro-aragorn
#42 interdiff-2962569-20-42.txt1.78 KBdmytro-aragorn
#42 search-api-image-styles-urls-for-media-2962569-42.patch9.12 KBdmytro-aragorn
#38 interdiff-2962569-20-38.txt1.72 KBnaveenvalecha
#38 interdiff-2962569-36-38.txt296 bytesnaveenvalecha
#38 2962569-38.patch9.35 KBnaveenvalecha
#36 interdiff-2962569-34-36.txt1.12 KBnaveenvalecha
#36 interdiff-2962569-20-36.txt1.43 KBnaveenvalecha
#36 2962569-36.patch9.06 KBnaveenvalecha
#34 interdiff-2962569-32-33.txt4.46 KBnaveenvalecha
#34 interdiff-2962569-29-32.txt574 bytesnaveenvalecha
#34 interdiff-2962569-26-29.txt785 bytesnaveenvalecha
#34 interdiff-2962569-20-34.txt597 bytesnaveenvalecha
#34 interdiff-2962569-20-26.txt9.22 KBnaveenvalecha
#34 2962569-34.patch9.06 KBnaveenvalecha
#33 search-api-image-styles-urls-2962569-33.patch10.08 KBwouterbohlken
#32 search-api-image-styles-urls-2962569-32.patch8.68 KBwouterbohlken
#31 search-api-image-styles-urls-2962569-31.patch8.68 KBwouterbohlken
#29 search-api-image-styles-urls-2962569-29.patch8.66 KBdmytro-aragorn
#26 search-api-image-styles-urls-2962569-26.patch8.47 KBdmytro-aragorn
#20 search-api-image-styles-urls-for-media-2962569-20.patch9.01 KBkyberman
#19 search-api-image-styles-urls-2962569-18.patch8.41 KBcdykstra
#18 search-api-image-styles-urls-2962569-18.patch8.4 KBcdykstra
#16 search-api-image-styles-urls-for-media-2962569-15.patch9 KBharika gujjula
#14 search-api-image-styles-urls-for-media-2962569-14.patch9.09 KBharika gujjula
#12 search-api-image-styles-urls-2962569.patch8.4 KBkilles@www.drop.org
#6 search-api-image-styles-urls-2962569-4.patch9.06 KBkasperg
#3 search-api-image-styles-urls-2962569-3.patch7.72 KBkasperg
#2 search-api-image-styles-urls-2962569-2.patch0 byteskasperg

Comments

dubcanada created an issue. See original summary.

kasperg’s picture

Status: Active » Needs review
StatusFileSize
new0 bytes

Here is a patch which tries to implement support for indexing image style variants of image fields.

It is implemented as a processor which can be enabled. When enabled you can add image style url fields where each field can be configured to use a combination of a image field and image style.

When items are indexed the selected image style is used to generate an absolute url for each image file in the specified field.

kasperg’s picture

StatusFileSize
new7.72 KB

Borked patch. Let's try again.

The last submitted patch, 2: search-api-image-styles-urls-2962569-2.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 3: search-api-image-styles-urls-2962569-3.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

kasperg’s picture

Here is an updated patch.

Compared to #3 the indexed url is now relative instead of absolute. Generating absolute urls from CLI which is a typical use case does not work out well.

The patch also includes changes which will hopefully make tests run a little better.

borisson_’s picture

Status: Needs work » Needs review

Setting to needs review to have the testbot take a look at this.

Status: Needs review » Needs work

The last submitted patch, 6: search-api-image-styles-urls-2962569-4.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

kasperg’s picture

Tests still fail. I cannot to decipher why based on the testbot output.

If there is interest in this contribution and anyone can point me in the right direction I can take another look.

it-cru’s picture

@kasperg: I've tried your patch, but I only see checkbox to enable 'Image style URL' processor. I don't get the settings form for selecting image styles.

I've a node with an image field (indexed as object, image file URI and image relative URL).

Does I miss something?

killes@www.drop.org’s picture

Yep, you did. ;)

The fields can be added in the "General" section.

Works fine.

killes@www.drop.org’s picture

This is the same patch as #6 with the transformation to relative URLs removed, which I think is the preferred way to have URLs in a search index.

Chris Gillis’s picture

This looks great. Any way to get it working with media entities? The data source will have no image field, because the image field is on a media entity linked to using entity reference.

harika gujjula’s picture

Status: Needs work » Needs review
StatusFileSize
new9.09 KB

Thanks @killes@www.drop.org. Patch works awesome.
Adding a patch which provides seperate Processor to support Media Image fields.

Status: Needs review » Needs work

The last submitted patch, 14: search-api-image-styles-urls-for-media-2962569-14.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

harika gujjula’s picture

Status: Needs work » Needs review
StatusFileSize
new9 KB

Re-adding the patch after fixing the coding standards.

Status: Needs review » Needs work
cdykstra’s picture

Patch in #12 works great in Drupal 8.9.13. This patch fixes the deprecated code in preparation for Drupal 9

cdykstra’s picture

Ugh, missed adding the changes...try this again

kyberman’s picture

Thank you very much Harika, your Media-related patch works great, but I needed to replace "entity.manager" service by "entity_type.manager", so it's now working with D9 too.

naveenvalecha’s picture

Component: General code » Plugins
Status: Needs work » Needs review

Setting to Needs review to see what testbot thinks

The last submitted patch, 12: search-api-image-styles-urls-2962569.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

The last submitted patch, 19: search-api-image-styles-urls-2962569-18.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

The last submitted patch, 18: search-api-image-styles-urls-2962569-18.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work
dmytro-aragorn’s picture

In my case a Search API index has fields from node and taxonomy entity types and ImageStyleUrlProperty:getImageFields() is failed once it tried to merge the fields properties via array_merge_recursive() function. So I've changed a bit the logic of ImageStyleUrlProperty:getImageFields()

naveenvalecha’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 26: search-api-image-styles-urls-2962569-26.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

dmytro-aragorn’s picture

Status: Needs work » Needs review
StatusFileSize
new8.66 KB

Another issue was found - if any of the selected entity types in the Search API Index don't have a field which was set for Image style URL then indexing is failed.
So need to ensure that field existing in the entity before processing it value.

Here is an updated patch which solve that issue.

Status: Needs review » Needs work

The last submitted patch, 29: search-api-image-styles-urls-2962569-29.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

wouterbohlken’s picture

This patch failed on Drupal 9, added a 'locked' property in the annotation. After adding this, the field was added.

wouterbohlken’s picture

StatusFileSize
new8.68 KB

Missed one change in my previous patch, here is the fix.

wouterbohlken’s picture

Add support for images included using media entity references.

naveenvalecha’s picture

Fixing the tests. I'm using the #20 as a starting point here.
Still, the patch needs improvement. I've also attached diffs of the patches attached between #20 and #34

Status: Needs review » Needs work

The last submitted patch, 34: 2962569-34.patch, failed testing. View results

naveenvalecha’s picture

Status: Needs work » Needs review
StatusFileSize
new9.06 KB
new1.43 KB
new1.12 KB

Here's the updated patch

Status: Needs review » Needs work

The last submitted patch, 36: 2962569-36.patch, failed testing. View results

naveenvalecha’s picture

Status: Needs work » Needs review
StatusFileSize
new9.35 KB
new296 bytes
new1.72 KB

Fixed the tests by defining its dependency on the media module as a temporary fix. Checked with @borisson_ about the direction of the patch and his feedback

Status: Needs review » Needs work

The last submitted patch, 38: 2962569-38.patch, failed testing. View results

drunken monkey’s picture

Title: Image Styles » Index image style URLs
Issue tags: +Needs tests

Thanks for proposing this new feature!
As there is obviously enough interest in this, sure, we can add this new processor.

However, in addition to fixing this with regards to existing tests, this should also get its own set of tests, as all new processors (or new code in general).

naveenvalecha’s picture

This will be fixed with #3273159: Search API token field processor. Looking forward to the feedback on that issue.

dmytro-aragorn’s picture

Patch from #38 doesn't applied for me and #20 has an issue with opening edit form.
So I've made a re-roll of the patch from #20 with a fix for edit form.

dmytro-aragorn’s picture

Status: Needs work » Needs review
StatusFileSize
new9.31 KB
new810 bytes

The same issue was reached as in #29 - an error is appear during indexing if the field doesn't exists in any of entity type from the index.
Here is an updated patch with prevent the error during indexing.

Status: Needs review » Needs work

The last submitted patch, 43: search-api-image-styles-urls-for-media-2962569-43.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

cslevy’s picture

Something is not ok with this patch.

When this was introduced

$container->get('entity_type.manager')->getStorage('media'),

if the Media module is not enabled, this breaks the whole search api functionality.

dmytro-aragorn’s picture

Status: Needs work » Needs review
StatusFileSize
new9.93 KB
new9.82 KB

I've fixed the issue with the patch usage when the Media module is disabled and also corrected syntax for PHP 8.2 usage.

naveenvalecha’s picture

Do we need this issue?
Can this use case not be solved with genric Search API "Custom value" Property?

@drunken monkey

Are you still open to support Image style Urls separately OR in favour of "Custom value" Property to close it ?

drunken monkey’s picture

Status: Needs review » Postponed (maintainer needs more info)

If this can be resolved by just using the existing “Custom value“ field, then I’m in favor of closing this as “won’t fix” (or maybe “outdated”).

naveenvalecha’s picture

I confirm. We're indexing the image styles with “Custom value“ field.

drunken monkey’s picture

Status: Postponed (maintainer needs more info) » Closed (won't fix)

OK, then let’s just close this. Thanks again!

jonathan_hunt’s picture

It would be helpful if this issue included specific documentation on how to replace the MediaImageStyleUrl processor plugin with "Custom value" as claimed above. With Custom value I can select tokens related to node image media but no token values related to custom image styles.

osopolar’s picture

I successfully replaced our Image style URL fields with Custom value fields.

For each existing image-style field, I created a corresponding custom value field with the same field name, only prefixed with custom_value__. The custom value uses a token like [node:field_teaser_media:entity:field_image:teaser:url].

After adding the first field, I rebuilt the tracking information and saved some demo content. I then verified that the custom value field returned the same URL as the existing image-style-url field. Once that worked, I migrated the remaining fields directly in the search_api.index.node_index.yml config file and re-imported the configuration. Finally, I compared the indexed values before and after the migration.

Finding the correct token was a bit tricky because the token tree depth under /admin/help/token is limited. I first navigated from node → teaser image → media → image, which gave me [node:field_teaser_media:entity:field_image]. Then I checked the media token tree via media → image → teaser (= image style) → URL, which gave me [media:field_image:teaser:url]. Combining both paths resulted in the final token [node:field_teaser_media:entity:field_image:teaser:url].

This worked for our media-reference image fields and produced the same indexed image-style URLs as the old processor fields.

I also tested a multi-value media reference field. The old Image style URL processor indexes all referenced images as separate values in the same Search API field. It was not working with Custom value fields + token. A token like the one above resolves, but only for the first/default referenced media item. Iterator-style tokens such as [node:field_images:*:entity:field_image:teaser:url] or [node:field_images:array:entity:field_image:teaser:url] did not resolve in my test.

There is a related Token issue: #3115486: Provide array of primitive property values on multivalue field. With the patch from that issue it might work (therefore leaving current issue status "Closed (won't fix)"), but I have not tested it.