Jump to content

Selector to search through Select Option Field titles in multiple languages


Recommended Posts

I'm looking for a way to search multilanguage titles in a couple of select option fields. I have looked through some threads here but couldn't find a solution that works for my specific problem. Any help is very much appreciated!

My setup: I have a multilanguage site with two languages, English and German. The default language is German. I'm building a search form for the template "job_offer" that has a couple of category fields. Those fields are built with the Select Options FieldType Module. They are categories for the region, industry, department et c. for the job offer. They have titles defined in German and English. For example, for the field "offer_region" there are the options "Austria", "Switzerland" and "Germany" that have the German country names as their German option titles.

The search field is supposed to search for text matches in the job description and category field titles. This is the selector is have so far:

$pages->find("template=job_offer, title|offer_body|offer_region|offer_rank%={$input->whitelist('search')}");

While the selector works for the title and offer_body fields, it only matches the German Select Option titles. For example, there's a job offer with the area set to "Austria" ("Österreich" in German). When I search for "Österreich", it shows up as expected. However, when I search for "Austria", I get know results, regardless of whether the current user language is English or German (note that the default language is German on this site). Any ideas how to get it to work?

Ideally, I would like to be able to filter through the Select Option titles in every language, i.e. I get the same results when I search for "Austria" or "Österreich" regardless of the current user language. However, for the moment I would be happy enough with a solution that will yield a result both when I search for "Austria" (while the user language is English) and when I search for "Österreich" (while the user language is German).

Thanks in advance!

Link to comment
Share on other sites

Did you use the value|title approach in your setup? Last section here: https://processwire.com/api/modules/select-options-fieldtype/#defining-options

One workaround / solution would be to use value|title, and let the region be a search-filter in the frontend, not available for freetext search.

My preferred setup for such things is to use page-reference fields instead of input field select options. And also, to only make text(area) fields searchable - category-type stuff (like offer_region) only queryable in the frontend via a filter (radio buttons, select, checkbox...). But I can understand that if you've already got a live site with lots of real data in your PW installation, it's not so easy to refactor everything without the potential danger of data-corruption.

Link to comment
Share on other sites

@dragan Thanks for the reply! I do have some select-options on the filter page, but I need those fields to be searchable through fulltext search unfortunately. I have used the value|title approach, but since I need to support fulltext search, searching the values instead of the titles is not an option. I'll probably use pages from the start the next time, but for now I have to work with what I got.

Regarding @heldercervantes's comment, I actually found a solution to that. Turns out that while the magic getter for $field->title and $field->value always return the default values, the proper methods $field->getTitle() and $field->getValue() return the correct values in the current user language. I did post a reply, it never got accepted though ..

Still, I'm not sure how to get this fulltext search to work with the current setup.

  • Thanks 1
Link to comment
Share on other sites

Ok, I couldn't figure out a way to do this with only selectors, so I built a custom solution. In case anyone has the same problem, here's my approach:

I created a hidden text field "offer_taxonomy_cache" and hooked a function after the saveReady hook to automatically populate this field with a space-seperated list of the values of the selected options for the fields that I want to search, in both languages. This is somewhat crude, but has the advantage that it's even more performant than using the select option fields themselves, as this way only one table has to be searched for matches. It's basically the same idea as the FieldType Cache core module, but this one didn't work for me, since it only saved the ID's of the selected options, leaving me with the same problem.

Here's my code:

// /site/ready.php

/**
 * Write taxonomy values to the cache field.
 */

$pages->addHookAfter('saveReady', function(HookEvent $event){

    // page to be processed
    $page = $event->arguments(0);

    // only hook onto job offers
    if ($page->template->name !== 'job_offer') {
        return;
    }

    $page->of(false);
    $user = wire('user');

    // all the fields to be included in the text cache
    $fields_to_cache = ['offer_area', 'offer_sector', 'offer_region', 'offer_rank', 'offer_type'];
    $taxonomy_cache_text = '';

    // loop over all fields
    foreach ($fields_to_cache as $field) {
        $fields = $page->get($field);
        // save the current language for later
        $current_language = $user->language;
        foreach (wire('languages') as $language) {
            // set the user language and loop over all selected options, then
            // add the title in the current language to the taxonomy cache text
            $user->language = $language;
            foreach ($page->get($field) as $option) {
                $taxonomy_cache_text .= ' ' . $option->getTitle();
            }
        }
        // reset the language
        $user->language = $current_language;
    }
    // trim the leading space
    $taxonomy_cache_text = trim($taxonomy_cache_text);

    // write the value to the cache field of the page
    $page->offer_taxonomy_cache = $taxonomy_cache_text;

    // write a log message
    wire('log')->message("Wrote taxonomy cache for page {$page->title} ({$page->id}): \"{$taxonomy_cache_text}\"");

});

One problem with this is the triple nested loop, so this might become quite slow for sites with many languages, select option fields and options per field. But in this case using a real page for the select options would be preferable anyway, as suggested. Thanks @dragan for your help!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...