How to access select option translations from blueprint?

I have a select field in the panel where I implemented translation directly in the blueprint like described in the docs.

In my case, the field are several categories for a FAQ stored as Question-Pages - like “About us” or “Service ABC”.

When displaying the FAQ categories in the FAQ template, I use a nested foreach loop to go through the categories and questions for each category:

// Group all questions per category
$categories = page('faq')->children()->groupBy('category');

foreach($categories as $category => $questionsPerCategory):?>

<div><h5><?= ucwords($category, " \t\r\n\f\v\-") ?></h5></div>

<?php  $catID++; endforeach ?>

When I now output the $category, it always displays the definition of the select option as defined in the question page, not the translation (as it does in the panel).

Is there an easy way to access the translation? I tried the cookbook recipe for accessing field options but could not wrap my head around the idea of how to make this work without actually accessing the actual page.

I hope my question makese sense?

Thanks
Andreas

Yes, that’s what you’d have to do, so get the translations from $category->blueprint(). Might make sense to only do this once. Or store what you have in your blueprint in the language translations and get it from there. Which would mean duplicating the work you have done in the blueprints, but I think the performance is better.

Thanks but this option is real ugly. I need to manually convert strings to uppercase because for whatever reason the category is outputted in lowercase but the array keys are not so that leads to things like

$sampleQuestionPage = page('faq/some-example-question');
$field = $sampleQuestionPage->blueprint()->field('category');
<?= ucwords($field['options'][ucwords($category, " \t\r\n\f\v\-")], " \t\r\n\f\v\-"); ?>

which just screams BREAK MY WEBSITE! :smiley:

I will find another solution or just ignore it for the time being, thanks!

Don’t know what you are doing here with the uppercasing and replacing.

The correct code would look like this:

$optionLabel = $page->blueprint()->field('options')['options'][$page->options()->value()][$kirby->language()->code()];
echo $optionLabel;

But if I were you, I’d go for translation variables and then use them also in your blueprint, like described in the docs: Select | Kirby CMS, second example.

Yes thank you, that is what I will most likely do :+1:

@texnixe I kept thinking about why my approach did not work and not sure but maybe I found another bug :slight_smile: Hear me out:

My approach of getting the categories of all published Question pages was:

$categories = page('faq')->children()->groupBy('category');

Which outputs the following:

            object(Kirby\Cms\Collection)#508 (3) {
  [0]=>
  string(16) "managed service"
  [1]=>
  string(11) "commercials"
  [2]=>
  string(33) "some training example"

And this is where the Hund lies begraben, no?

Why are they all lower-case? They are not:

When I

$sampleQuestionPage->blueprint()->field('category');

The following is outputted:

array(6) {
  ["About the Company"]=>
  array(2) {
    ["en"]=>
    string(17) "About Companyname"
    ["de"]=>
    string(17) "Über Firmenname"
  }
  ["Managed Service"]=>
  array(2) {
    ["en"]=>
    string(16) "Managed Service"
    ["de"]=>
    string(16) "Managed Service"
  }
  ["Some Training Example"]=>
  array(2) {
    ["en"]=>
    string(33) "Some Training Example"
    ["de"]=>
    string(33) "Ein Beispieltraining"
  }
[....]
}

That is why I have to use the ucwords stuff in the first place and that is why I would have to use it to be able to address the right key in the array.

What do you think?

Ok, but frankly, I would only store lowercase values, then you don’t run into this issue.

Well, yes that is a solid workaround :sweat_smile:

I used upper case ones because I did not really think about the translations when doing this but you are right, this is the easiest fix. The nice looking version is stored in the translation anyway :+1:

I really love Kirby but the whole upper/lower-case thing is something I have issues with repeatedly g

The issue with the lower-cased grouping values will be fixed in the next release.

1 Like