Jump to content

How to use language-alternate fields inside selectors?


webaff
 Share

Recommended Posts

Hi, 

I want to use language-alternate fields inside selectors for manipulating PageArrays, i.e. find(), sort() or filter().

I have set up a multi-lingual file upload with the file field "fileField" and its language alternate fields "fileField_lang1", "fileField_lang2", ...

This works as expected:

$matches->sort("-fileField.modified");

While this:

$matches->sort("-fileFile_lang1.modified");

...throws an error: "Recoverable Fatal Error: Argument 1 passed to ProcessWire\PageArray::getItemPropertyValue() must be an instance of ProcessWire\Wire, boolean given, called in ..."

Maybe I have to turn off outputFormatting? But how would I do that in this context?

Any help is well appreciated, thx.

Link to comment
Share on other sites

Hi @webaff

Have you tried to switch user language to lang1?

<?php
$currentLanguage = $user -> language; // remember language
$langauge = $languages-get('lang1');
$user -> language = $language;
$matches->sort("-fileField.modified");       
$user -> language = $currentLanguage; // restore language
?>

Maybe there's a better way to do that.

Link to comment
Share on other sites

@Zeka, thanks for your reply. I did try that already with no success.

I will give some more insight into my setup:
It's a multilingual site with the concept of _init.php/_func.php/template/_main.php

Inside my template, I get my PageArray together...

$matches = $pages->find("template=file");
$matches = $matches->sort("-fileField.modified");
$output = renderFileList($matches);

and pass it on to _func.php, where the output ist generated and returned:

function renderFileList(PageArray $items) {
  out = '';
  foreach($items as $item) {
    $out .= $item->fileField->modified;
  }
  return $out;
}

This returns the timestamp of the language specific file. If user language is not default, this magically gets fileField_altlang, as expected from the concept of language-alternate fields.

BUT: the results are not sorted by the timestamp of the fileField_altlang but are sorted by the timestamp of the default fileField, no matter what the current user language is. It seems to me, that the concept of language-alternate fields does not work inside Selectors.

I hope that someone can follow, reproduce or disprove my findings. Thx.

Link to comment
Share on other sites

On 14/07/2017 at 9:24 AM, webaff said:

This works as expected:


$matches->sort("-fileField.modified");

While this:


$matches->sort("-fileFile_lang1.modified");

...throws an error: "Recoverable Fatal Error: Argument 1 passed to ProcessWire\PageArray::getItemPropertyValue() must be an instance of ProcessWire\Wire, boolean given, called in ..."

This error is likely caused by a page in your $matches PageArray having an empty file field. If there is no file then you cannot get the modified timestamp, hence the error. The issue is not related to language alternate fields and would also occur in your default file field if one was empty.

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

×
×
  • Create New...