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

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.

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.

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:

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.

1 Like

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?

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?

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.

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?

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

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 ?>

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

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)

Not working for me.

@ashupatel1990 Do you have a field called text? What exactly is not working for you?

image
I have fields shown in above image.
now i wanted to get field names in template file. like (Name, Stadt and Jahr)
i wanted to show those labels in table header.

To get the name field:

<?php echo $page->blueprint()->field('archivs')['fields']['name']['label'] ?>

In the same way for the other fields within the structure.

dump($page->blueprint()->field('archivs'))

returns an array.

1 Like