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)?
| Comment | File | Size | Author |
|---|---|---|---|
| #46 | interdiff-2962569-43-46.txt | 9.82 KB | dmytro-aragorn |
| #46 | search-api-image-styles-urls-for-media-2962569-46.patch | 9.93 KB | dmytro-aragorn |
| #38 | interdiff-2962569-20-38.txt | 1.72 KB | naveenvalecha |
| #38 | 2962569-38.patch | 9.35 KB | naveenvalecha |
| #20 | search-api-image-styles-urls-for-media-2962569-20.patch | 9.01 KB | kyberman |
Comments
Comment #2
kasperg commentedHere 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.
Comment #3
kasperg commentedBorked patch. Let's try again.
Comment #6
kasperg commentedHere 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.
Comment #7
borisson_Setting to needs review to have the testbot take a look at this.
Comment #9
kasperg commentedTests 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.
Comment #10
it-cru@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?
Comment #11
killes@www.drop.org commentedYep, you did. ;)
The fields can be added in the "General" section.
Works fine.
Comment #12
killes@www.drop.org commentedThis 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.
Comment #13
Chris Gillis commentedThis 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.
Comment #14
harika gujjula commentedThanks @killes@www.drop.org. Patch works awesome.
Adding a patch which provides seperate Processor to support Media Image fields.
Comment #16
harika gujjula commentedRe-adding the patch after fixing the coding standards.
Comment #18
cdykstra commentedPatch in #12 works great in Drupal 8.9.13. This patch fixes the deprecated code in preparation for Drupal 9
Comment #19
cdykstra commentedUgh, missed adding the changes...try this again
Comment #20
kybermanThank 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.
Comment #21
naveenvalechaSetting to Needs review to see what testbot thinks
Comment #26
dmytro-aragornIn 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 viaarray_merge_recursive()function. So I've changed a bit the logic ofImageStyleUrlProperty:getImageFields()Comment #27
naveenvalechaComment #29
dmytro-aragornAnother 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.
Comment #31
wouterbohlken commentedThis patch failed on Drupal 9, added a 'locked' property in the annotation. After adding this, the field was added.
Comment #32
wouterbohlken commentedMissed one change in my previous patch, here is the fix.
Comment #33
wouterbohlken commentedAdd support for images included using media entity references.
Comment #34
naveenvalechaFixing 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
Comment #36
naveenvalechaHere's the updated patch
Comment #38
naveenvalechaFixed 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
Comment #40
drunken monkeyThanks 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).
Comment #41
naveenvalechaThis will be fixed with #3273159: Search API token field processor. Looking forward to the feedback on that issue.
Comment #42
dmytro-aragornPatch 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.
Comment #43
dmytro-aragornThe 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.
Comment #45
cslevy commentedSomething is not ok with this patch.
When this was introduced
if the Media module is not enabled, this breaks the whole search api functionality.
Comment #46
dmytro-aragornI've fixed the issue with the patch usage when the Media module is disabled and also corrected syntax for PHP 8.2 usage.
Comment #47
naveenvalechaDo 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 ?
Comment #48
drunken monkeyIf 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”).
Comment #49
naveenvalechaI confirm. We're indexing the image styles with “Custom value“ field.
Comment #50
drunken monkeyOK, then let’s just close this. Thanks again!
Comment #51
jonathan_hunt commentedIt 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.
Comment #52
osopolarI 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.ymlconfig 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/tokenis limited. I first navigated fromnode → teaser image → media → image, which gave me[node:field_teaser_media:entity:field_image]. Then I checked the media token tree viamedia → 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.