Jump to content

Setting a limit for pagination when using $pagearray->append() and multi-lingual content


johnstephens
 Share

Recommended Posts

Hi!

I'm working on a search page for a multi-lingual website. I want to start by showing matches for the user's current language, then append matches for other languages.

Here's what I currently have:

$current_lang = user()->language;

// $q = the query input, sanitized as a selector

$matches = pages()->find("title|headline|subtitle|topics|summary%=$q, has_parent!=2, limit=$limit");                 

foreach (languages() as $language) {
    if ($language !== $current_lang) {
		user()->language = $language;
    	$matches->append(pages()->find("title|headline|subtitle|topics|summary%=$q, has_parent!=2, limit=$limit"));
	}
}
user()->language = $current_lang;

This appears to have two issues:

  1. The pagination gets all wonky, showing more pages of results than actual content; and, (I think this is related)
  2. It doesn't seem to append any actual content from the other languages.

When I noticed the pagination bug, I realized it could be because I set the limit selector twice. I removed it from the selectors above and added another line after appending the matches from other languages as follows:

$matches = $matches->find("limit=$limit");

Of course, that didn't work either.

I'm curious, what is the correct way to set a limit for a pagearray which has been assembled with append() (or prepend()). And what is the correct way to query all content, while giving priority to content in the current language?

Thanks in advance!

Link to comment
Share on other sites

Ok, I've made some headway and wanted to post a field note.

First, I realized the selector string in the above text would only search the default language fields for each page, which isn't useful for getting content from the other languages. I created a function to append the language ID to each field:

$search_fields =
  [ 'title'
  , 'headline'
  , 'subtitle'
  , 'summary'
  ];

function fields_selector_string ($search_fields, $language) {
    $lang_search_fields = $language->isDefault()
        ? $search_fields
        : preg_filter('/$/', '.data' . $language, $search_fields);
    return implode('|', $lang_search_fields);
}

Then I replaced that part of the selector string with the language-aware version:

$matches = pages()->find(fields_selector_string($search_fields, user()->language) . "%=$q, has_parent!=2, limit=$limit");

bd($matches, 'Current lang matches'); // Dump the matches for the current language using TracyDebugger

foreach (languages() as $language) {
    if ($language === $current_lang) continue;
    user()->language = $language;
    $lang_matches = pages()->find(fields_selector_string($search_fields, $language) . "%=$q, has_parent!=2, limit=$limit");
    bd($lang_matches, 'Otras lang matches'); // Dump mathces for other languages using TracyDebugger
    $matches->append($lang_matches);
    user()->language = $current_lang;
}

The good news is, the Tracy barDumps list a reasonable number of matches for each language, and spot checking the pages included, they appear to be correct.

The trouble is, pagination is still messed up.

For one, for a sample search query, "Current lang matches" has 30 items, and "Otras lang matches" has 17. But after appending them, $matches->getTotal() shows 42.

But the bigger issue is that the pagination shows links to 5 pages of results, of which only the first 3 are populated.

My hunch is, the page will only display results in the current language, even though the $matches PageArray has additional items in it.

Does anyone here know how to display the content from the non-current language that is appended?

Thanks in advance!

Link to comment
Share on other sites

  • 8 months later...

I too am having this issue. Our use case is doing a simple site search (page content contains search query) and then appending a separate search for one particular template that we want to de-rank to the bottom of the results.

Has anyone got any leads on fixing this?

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