Elxis CMS Forum

Support => General => Topic started by: seadhna on June 07, 2021, 16:00:35

Title: Order tag listing by correct language titles?
Post by: seadhna on June 07, 2021, 16:00:35
When we tag articles in a second language and then visit the tag listing on that language version of the website, the articles are ordered by the default language spelling, not by the language we are viewing. Is it possible to change this?

e.g. imagine four articles named in English (default language) as 1) Ethiopia 2) Uzbekistan 3) Zambia 4) Zimbabwe
Then on a DE version of the site, we name the articles as 1) Äthiopien 2) Usbekistan 3) Sambia 4) Simbabwe

Now we tag all four articles in English as 'countries' and in German as 'länder'. If we visit: www.example.org/tags.html?tag=countries
we can see the four articles ordered alphabetically: 1) Ethiopia 2) Uzbekistan 3) Zambia 4) Zimbabwe

However, if we visit the tag page in DE: www.example.org/de/tags.html?tag=länder
the four articles are not listed in correct alphabetical order - instead they are listed according to the English titles.
Articles SHOULD be ordered: 1) Äthiopien 2) Sambia 3) Simbabwe 4) Usbekistan
but they are not. They ARE ordered: 1) Äthiopien 2) Usbekistan 3) Sambia 4) Simbabwe
which doesn't make sense. Is there a way to make sure tag listings are ordered alphabetically in the correct order for that language?
Title: Re: Order tag listing by correct language titles?
Post by: seadhna on June 07, 2021, 16:10:03
I just realized I stated this incorrectly: tag listings are ordered by DATE. Is it possible to order a tag listing alphabetically instead?
I could date the articles below so that they appear in correct alphabetical order in English, but the order won't change in other languages...
Title: Re: Order tag listing by correct language titles?
Post by: datahell on June 07, 2021, 20:01:49
The ordering method is fixed. They are ordered by creation date in descending order (newer first).

It is not that easy to create a perfect ordering in mixed multilingual strings. This is because PHP has a problem here and to bypass it you need either to develop a special order class or use PHP collator which is not enabled by default in PHP. I will tell you what to do but it will not work perfect on mixed content because the ordering method does binary comparisons, not string comparisons.

Open this file:
components/com_content/models/content.model.php
Go to lines 548-549, you will find this:
unset($articles);
return $rows;

Add between them a new line so at the end you will have this:
unset($articles);
uasort($rows, array('contentModel', 'customOrderByName'));
return $rows;

One line below and after the  closing bracket "}" add this method:

public static function customOrderByName($a, $b) {
   return strcmp($a->title, $b->title);
}

In English it works fine. For Greek language the result is that it displays the order correctly but in reverse order. This is because the comparison function works in binary mode. I believe the same will happen in French, German, Russian, Chinese, and any other language that has multi-byte characters over the ASCII ones.

My advice is not to change it. It is better to order the articles in tags page by date rather than by title. The newer articles will be displayed first and this is better.
Title: Re: Order tag listing by correct language titles?
Post by: seadhna on June 08, 2021, 12:41:37
Hmm, yes, it works in English but not for the German version. Ok, I follow your advice and don't change it. Thanks!