Support from Acquia helps fund testing for Drupal Acquia logo

Comments

lauriii created an issue. See original summary.

lauriii’s picture

lauriii’s picture

Title: Filter search results for current page » Filter search results for current language

Wtf have I been thinking..

borisson_’s picture

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.

drunken monkey’s picture

Please 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.

snufkin’s picture

Although 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.

snufkin’s picture

Updating 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.

swentel’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Yeah, some tests here would be nice.
Tests are now finally working also :)

abdallah_exe’s picture

Hi, 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

drunken monkey’s picture

Hi, 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?

First 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 contains search_api_page.module, using patch -p1. It will modify the src/Controller/SearchApiPageController.php file (relative to that folder).

dbgilbert’s picture

borisson_’s picture

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.

crtlf’s picture

Patch #7 did not worked, but #11 worked for me on :

  • Drupal 8.3.7
  • Search API 8.x-1.4
  • Search API Pages 8.x-1.0-alpha11
StryKaizer’s picture

Status: Needs work » Reviewed & tested by the community

Just tested #11 which works as expected.
Setting RTBC since we seem to have no time for tests ;-)

borisson_’s picture

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.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 15: 2649332.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

drunken monkey’s picture

</code><ol>

<li>
<code>
+++ b/src/Controller/SearchApiPageController.php
@@ -74,6 +74,10 @@ class SearchApiPageController extends ControllerBase {
+      // Add filter for current language.
+      $langcode = \Drupal::service('language_manager')->getCurrentLanguage()->getId();
+      $query->addCondition('search_api_language', $langcode);

Not 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.)

  • +++ b/src/Controller/SearchApiPageController.php
    @@ -95,6 +99,9 @@ class SearchApiPageController extends ControllerBase {
    +        // Get the translated entity.
    +        $entity = \Drupal::service('entity.repository')->getTranslationFromContext($entity, $langcode);
    +
    

    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.)

  • +++ b/tests/src/Functional/LanguageIntegration.php
    @@ -0,0 +1,84 @@
    +class LanguageIntegration extends FunctionalTestBase {
    

    Test class names have to end in *Test.

  • +++ b/tests/src/Functional/LanguageIntegration.php
    @@ -0,0 +1,84 @@
    +    $bird_node->addTranslation('nl', ['title' => 'bird: Havik']);
    +    $bird_node->addTranslation('es', ['title' => 'bird: Halcon']);
    

    You still need to save the node after that.

  • The last submitted patch, 17: 2649332-17--language_filter--tests_only.patch, failed testing. View results

    borisson_’s picture

    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.

    borisson_’s picture

    Since @StryKaizer set this to rtbc earlier, I guess we can commit this even without an explicit +1 on #17.

    borisson_’s picture

    Status: Needs review » Fixed

    Committed and pushed. Thanks everyone!

    Status: Fixed » Closed (fixed)

    Automatically closed - issue fixed for 2 weeks with no activity.