How to get in template select-based field label instead of value?

#1

Hello.

I cannot find the way to get labels from select or multiselect or other value+label designed fields.

At example, I have field values like this:

en: English
fr: Francais

I need to print English instead of en in my template, but cannot find the way to get it. There is “Kirby Architect” plugin, may be this helps, but this only for Kirby 2.x. May be I missing to view some API function?

Thank you.

#2

You can use $page->blueprint() as a basis and then take it from there, see this cookbook recipe:
https://getkirby.com/docs/cookbook/templating/using-blueprints-in-frontend

E.g. to access a single field:

$field = $page->blueprint()->field('text');
echo $field['label'];

In the same way, you can get other properties of the field, like the options.

#3

Thanks for the hint, but I wonder how to grab the correct language in my template, e.g.:

  company:
    extends: fields/menuitems
    label: 
      de: Unternehmen
      en: Company

I thought
<?=$field->label()?> would be enough. I could not find in the docs how to access the language code/locale of a blueprint. Would love to receive you hint!

my dump() - Info:

Array
(
    [label] => Array
        (
            [de] => Unternehmen
            [en] => Company
        ) ...

Could be a Quicktip in the Cookbook :wink:

#4

You can get it from the array like this:

$field = $page->blueprint()->field('text');
echo $field['label']['de'];

I also corrected my code above, because $field is an array not an object, so to get the label, you have to use the index, not string syntax.

#5

Ok. I thought that I missed something when it comes to selecting the correct label depending on the locale.
So I have to take care for myself in blueprints, whereas having a extra field in the panel would automate my translation and take care automatically. Right?

#6

Sorry, I probably haven’t had enough coffee yet, but I’m not following you? Your use case seems to be different from that of @azmg?

#7

Sorry too, seems I am the one that had not enough coffee. I understood the question differently - maybe as I wanted to understand it my way. ;o)
My usecase is to print the correct label for the locale language in my template and the label is defined in blueprint.

#8

Yes, but the above code should then work for you, with the difference that instead of hardcoding the locale you would use the language code of the current language?

#9

Exactly - I thought to have missed something like a function or helper.
Now I finally go on with your advice:

<?= $field['label'][$kirby->languagecode()]

thanks you for your patience

#10

Is it possible to do same with api JSON file? How can I get Text not Value ?
For example I have languages that is stored in json

[
  {"code":"cn","name":"Chinese"},
  {"code":"dk","name":"Dansk"},
  {"code":"de","name":"Deutsch"},
  {"code":"en","name":"English"},
  {"code":"es","name":"Español"},
  {"code":"fr","name":"Français"},
  {"code":"ge","name":"Georgian"},
  {"code":"hr","name":"Hrvatski"},
  {"code":"gr","name":"Ελληνικά"},
  {"code":"is","name":"Íslenska"},
  {"code":"it","name":"Italiano"},
  {"code":"jp","name":"Japanese"},
  {"code":"mk","name":"Macedonian"},
  {"code":"hu","name":"Magyar"},
  {"code":"nl","name":"Nederlands"},
  {"code":"no","name":"Norsk"},
  {"code":"pl","name":"Polski"},
  {"code":"pt","name":"Português"},
  {"code":"ru","name":"Русский"},
  {"code":"ro","name":"Română"},
  {"code":"sk","name":"Slovenčina"},
  {"code":"fi","name":"Suomi"},
  {"code":"se","name":"Svenska"},
  {"code":"tr","name":"Türkçe"},
  {"code":"bg","name":"Български"}  
] 

When I add it to review template it save them as

Languages: de, es, fr, nl, se, bg

How can I get Names in template?

<?php if($page->languages()->isNotEmpty()): ?>
    <div class="d-block">
        <p class="x-small text-uppercase mb-1">Languages</p>
        <span class="h6 mb-1 d-block">
    	<?php foreach($page->languages()->split() as $language): ?>
    	<span class="flag-icon flag-icon-<?= strtolower($language) ?> flag-icon-squared flag-lg mb-1" title="HERE I WOULD LIKE TO USE NAME"></span>
        <?php endforeach ?>
            </span>
    </div>
<?php endif ?>
#11

I’d turn that into an array and then use PHP array methods to get the appropriate value:

$json = F::read($kirby->root('index').'/country_codes.json');
$array = json_decode($json, true);
dump($array);

1 Like
#12

Yep, I done it in controller… little bit in different way.

$apicountries = json_decode(file_get_contents($site->url()."/api/countries.json"), true);
$apicurrencies = json_decode(file_get_contents($site->url()."/api/currencies.json"), true);
$apilanguages = json_decode(file_get_contents($site->url()."/api/languages.json"), true);

And in templeate I made this like this.

<?php if($page->languages()->isNotEmpty()): ?>
    <div class="d-block">
        <p class="x-small text-uppercase mb-1">Languages</p>
        <span class="h6 mb-1 d-block">
			<?php foreach($page->languages()->split() as $language): ?>
									
			<?php foreach($apilanguages as $item): ?>
			<?php if ($language == $item['code']): ?>
			<?php $apilanguage = $item['name']; ?>
			<?php endif ?>
			<?php endforeach ?>		   
										
			<span class="flag-icon flag-icon-<?= strtolower($language) ?> flag-icon-squared flag-lg mb-1" title="<?= $apilanguage ?>"></span>

        <?php endforeach ?>
            </span>
    </div>
<?php endif ?>

Not sure, that this is okay, but it works)