Attached patch adds support for indexing the labels of options lists. While search api automatically adds the labels of selected values in if the field gets fulltext indexed, sometimes its of advantage to have it as a separate field. My use case for that is providing a facet of a list field with allowed values being translated via i18n_field, whereas the facet is using the (translated) label as value (and in the URL).

(For adding in the i18n_field translation you'll need the latest entity api dev version for which I've just added support.)

So the attached patch does that by providing an alter callback, which provides a property everything having options lists declared. The code would work with arbitrary hierarchies, however unfortunately it turned out search api does not alter property info of the wrappers recursively, so it only works on the first level. Consequently, I've disabled hierarchy>1 support in the patch for now.

CommentFileSizeAuthor
d7_search_api_options_list_label.patch4.25 KBfago
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

drunken monkey’s picture

Component: Miscellaneous » Plugins
Category: Task » Feature request
Status: Needs review » Needs work

Thanks a lot for your suggestion and the patch. I can only see one problem:

+++ b/includes/callback_add_option_label.inc
@@ -0,0 +1,80 @@
+        $return["$selector--label"] = array(

To quote your own module's documentation (hook_entity_property_info()):

The name may only contain alphanumeric lowercase characters and underscores.

So please use something else as the suffix.

However, apart from this, I'm not sure this is generally useful enough to warrant addition to this module directly. So, unless others start requesting this, I don't think I will add this patch, sorry!
Someone should really finally start a "Search API Bonus" module/project for such things …

fago’s picture

search API bonus sounds like a good idea. Could you think of a good suffix to add? If we go with a valid name, we might have bad luck and end up with clashing names.

drunken monkey’s picture

search_api_bonus should be fine, I think. I can't think of any hook or functions that starts with "bonus".
But yes, that's of course always a problem in Drupal currently. Thanks god proper namespaces are coming … ;)

webflo’s picture

Version: 7.x-1.x-dev » 8.x-1.x-dev

Any ideas how to do it in D8 (in Search API Core) or do we need an additional module?

webflo’s picture

I think we could leverage \Drupal\Core\TypedData\OptionsProviderInterface here.

drunken monkey’s picture

Since it's not so simple anymore to get options lists in D8's Entity API, I guess the automatic indexing of options labels for fulltext fields got dropped in D8. For that, you should probably write a separate processor that does this. If it works well enough, we could then also add it to this module, I guess.