Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
It would be nice if search results were filtered for current language only.
Proposed resolution
Filter search results for current language only. We might want to make this configurable on the UI.
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#17 | 2649332-17--language_filter.patch | 3.57 KB | drunken monkey |
| |||
#17 | 2649332-17--language_filter--tests_only.patch | 2.59 KB | drunken monkey |
Comments
Comment #2
lauriiiComment #3
lauriiiWtf have I been thinking..
Comment #4
borisson_I think it makes sense to do this by default. we should probably add an integration test for this behavior as well though.
I don't see any test that confirms the behavior for multilingual at all so if you feel this should be handled in another issue that would make sense.
Anyway, the code change looks great.
Comment #5
drunken monkeyPlease note that the "Item language" field can now have any machine name, it doesn't have to be
search_api_language
. Therefore, this code might now throw an exception one some sites (and more later, once it becomes possible to change the field ID through the UI).In any case, generally I agree with this issue, though I feel it should be configurable. But the final decision is swentel's anyways.
Comment #6
snufkin CreditAttribution: snufkin at Acquia commentedAlthough this does filter for the langcode, the resulting entities do not display the translation, so entities will link to the default language. A small change of
$entity = \Drupal::service('entity.repository')->getTranslationFromContext($entity, $langcode);
fixes this.Comment #7
snufkin CreditAttribution: snufkin at Acquia commentedUpdating the patch with the suggested change above. This allows the content listing to show the links to the translations and uses the right language entity if that display is chosen.
Comment #8
swentel CreditAttribution: swentel as a volunteer commentedYeah, some tests here would be nice.
Tests are now finally working also :)
Comment #9
abdallah_exe CreditAttribution: abdallah_exe as a volunteer commentedHi, i am new to drupal...i wanted to limit my search result by the current language of my website. I have 'French' & 'English' language on my website. How do i go around to apply this patch?In what file do i add these line of codes?
Thanks
Comment #10
drunken monkeyFirst off: Are you actually using this module ("Search pages" on your "Extend" page), and not looking for information about the "Search" module included in Drupal itself?
If you're using this module, you should apply the patch from within the
search_api_page
folder, which also containssearch_api_page.module
, usingpatch -p1
. It will modify thesrc/Controller/SearchApiPageController.php
file (relative to that folder).Comment #11
dbgilbert CreditAttribution: dbgilbert at SEI Global Services Inc. commentededit: added org
Comment #12
borisson_I don't know why #11 was needed, in any case - we still need those tests. We should wait to write those tests until #2709953: Support the search api display plugin is committed, because that refactors them from simpletest to phpunit-based browser tests.
Comment #13
crtlf CreditAttribution: crtlf commentedPatch #7 did not worked, but #11 worked for me on :
Comment #14
StryKaizerJust tested #11 which works as expected.
Setting RTBC since we seem to have no time for tests ;-)
Comment #15
borisson_This is an attempt at a test, but it doesn't pass. I don't know if the problem is with the fix or with the test. But I'm assuming the problem is with the test.
Comment #17
drunken monkeyNot 100% sure, but doesn't the content language make more sense here than the interface language?
Also, it might make sense to only do that if there's more than one language available? Otherwise we waste performance on single-language sites. (In D7 we even had that filter configurable, but I guess that's "just use Views already!" territory of feature creep.)
This part shouldn't be necessary. The entity coming back will already have the appropriate language, since Search API items have a single fixed language.
(But then again, the whole loop body is wrong, no need to use entities at all.)
Test class names have to end in
*Test
.You still need to save the node after that.
Comment #19
borisson_Thank you very much helping write that test @drunken monkey! I think we can commit this as soon as someone comes back with positive manual test-results.
Comment #20
borisson_Since @StryKaizer set this to rtbc earlier, I guess we can commit this even without an explicit +1 on #17.
Comment #21
borisson_Committed and pushed. Thanks everyone!