Sort collection by translated value from category map

So last question I gotten it all to work nicely. Now just missing if possible, sorting.

Is it a easy Kirby native way to sortBy() the category using the final text? Also worth saying, the sort option works well, but I want sort on the final translated text value witch is stored in the config file sa an array.

May I ask for your input on this as well @texnixe?

Not sure if you follow me? Let me know if you need anything else of info?

How do you fetch the final translated value?

If you convert that into a custom page method or page model, you can sort by that method.

E.g as a page model

class ProductPage extends Page {
  public function getCategoryTranslation() {
    return c::get('job_category_name')[$this->category()->value()];
  }
}

Then you can sort your groups by this method:

$categories = $groups->sortBy('getCategoryTranslation')->groupBy('category');

(hope, no missing parenthesis or the like anywhere…)

This is the code snippet:

In my snippet:

<?php $categories = $groups->groupBy('category');
    foreach($categories as $category => $items): ?>

<h1><?php echo $categoryName[trim($category)]; ?></h1>
// Rest of the loop...

In my template:

<?php $categoryName = c::get('job_category_name'); ?>

In the config.php

c::set('job_category_name', [
  'option1' => 'Some title 1',
  'option2' => 'Some title 2',
  'option3' => 'Some title 3'
]);

Please see my edit above…

Did so but it did not work. No errors just no response.

Do I pass the value to the snippet correctly?

<?php snippet('overview', array('categoryName' => $categoryName, 'getCategoryTranslation' => $getCategoryTranslation)) ?>

Also is there a way to pass values from the snippet? Is that ever needed? (This is just good to know) :slight_smile:

There is no need to pass the page model method to the snippet.

Then I am a little confused since it did not work. :confused:

It’s a bit late now :sleeping:, if you want, you can send me the relevant parts of your project as zip or upload to GitHub, then I could look into this tomorrow.

1 Like

I will… tomorrow. Time for :zzz: here too. :wink:

I am presented with a new issue, regarding the sort order. I wish to control the order, but after adding the links for filtering each category I am faced with another issue. So how can I add sort order as a extra field in the config file? (Not sure how to add that to the array).

Also how will you go around getting it sorted right in the front-end? I am thinking that the easy way around this is to use numbers just, 01, 02, 03 etc.

How you have a awesome week @texnixe :smile:

I’m not sure I get it.

So you don’t want to sort by alphabetical order of the “translated” strings (Some title 1, Some title 2) anymore but the order these values have in within the array?

If you use numbers for your options like this:

options:
  1: Option 1
  2: Option 2

you can, of course, use these to sort by and no need to sort by a custom page model/method.

I move away from the alphabetical sorting to a number based sorting. Here is my config file with the “translated” array. Now is there a way to attach another value to the each to give them the sorting order? Like'sort' => 05 ?

// Job type names in the front-end :)
c::set('job_category_name', [
  'sand' => 'Sand, grus, gjørme',
  'fat' => 'Fett',
  'channels' => 'Åpene kanaler',
  'toilet' => 'Avløpsåpning',
  'debris' => 'Avleiringer',
  'cleaning' => 'Regelmessig rengjøring',
  'roots' => 'Røtter',
  'shaft' => 'Sjakt rengjøring',
  'objects' => 'Utstikkende objekter',
  'special' => 'Store og spesielle profiler'
]);

Do you follow me? Do I need an array inside the array?

Are you sure you do not mean “filter”? How can you sort something by “cleaning” as in your example?

I think this makes most sense. I will do this then. Cause then I do not add more issues then needed. :slight_smile:

c::set('job_category_name', [
  '1' => 'Sand, grus, gjørme',
  '2' => 'Fett',
  '3' => 'Åpene kanaler',
  '4' => 'Avløpsåpning',
  '5' => 'Avleiringer',
  '6' => 'Regelmessig rengjøring',
  '7' => 'Røtter',
  '8' => 'Sjakt rengjøring',
  '9' => 'Utstikkende objekter',
  '10' => 'Store og spesielle profiler'
]);
1 Like

The idea here is to group with a certain sort order of the categories, not filter. The original idea was to sort alphabetically based on the values of the c::set() array.

Thank you @texnixe! :blush: Of course in a perfect world I will have named urls. Cause this is also the param('1') in the url. So that is why I was thinking of this as a separate value. But thank you!

Worth mentioning is that if ever want to reorder this you also need to resave all the pages with the new value since this is also reflected in my category select dropdown in the panel.

That is it would be great in a future version to add that. :slight_smile:

Oh, I see. Then maybe this quick and dirty solution is not so ideal, after all, and a multi-dimensional array with a sort order attached to it would be the more future-proof solution.

c::set('job_category_name', [
  'sand' => [ 'value' => 'Sand, grus, gjørme',
              'order' => 1
            ],
 //...
]);
1 Like

Yes. You read my mind. :slight_smile: So how do I go around making the arrays and then how can I access/parse them in the front-end?

Then you would need this wonderful page method/model again, with the difference that you now have to deal with a multi-dimension array to grab the correct sorting value…

See above, I tend to have afterthoughts and then edit my answers to provide more detailed information.

And then I guess you want an adapted method as well… :thinking: But to enhance the learning effect, try it yourself first… and you will be proud of yourself if you get it to work :wink:.

1 Like