Select field: Display 'text' instead of value

I have a select field and want to show the ‘text’ instead of the value in the frontend. For instance, I want to show 30 × 40 cm instead of w30h40. How do I have to code my template in order to achieve this?

My blueprint:

size:
        label: Size
        type: select
        required: true
        options:
          w30h40: 30 × 40 cm
          w40h30: 40 × 30 cm
          w60h80: 60 × 80 cm
          w80h60: 80 × 60 cm
        help: Width × Height

My template:

<?php foreach (page('works')->children()->filterBy('template', 'painting') as $painting): ?>
<figure class="<?= $painting->size() ?>">
<a href="<?= $painting->url() ?>" target="_self"><img src="<?= $painting->image()->url() ?>" alt="<?= $painting->title() ?>"></a>
  <figcaption><?= $painting->title() ?>, <?= $painting->size() ?></figcaption>
</figure>
<?php endforeach ?>
```*emphasized text*

You can either query the value via the $page->blueprint() method, or you store those values somewhere else, in a single language site in an array in config, or in a multi-language site in the language files.

More info:

1 Like

Well, I am able to call the field’s label, type, required, help using the following PHP snippet but I am not able to call the text of the value of the selected option or the options at all:

<?= $painting->blueprint()->field('size')['label'] ?>

These snippets give different errors for instance:

<?= $painting->blueprint()->field('size')['options'] ?>
Whoops\Exception\ErrorException thrown with message "Array to string conversion"
<?= $painting->blueprint()->field('size')->options() ?>
<?= $painting->blueprint()->field('size')->options()['text'] ?>
Error thrown with message "Call to a member function options() on array"

How do I call the text of the stored value of the select field?

<?= $page->blueprint()->field('size')['options'][$page->size()->value()] ?? ''); ?>

Replace $page with your variable in the loop…

The error is thrown because you are trying to echo an array.

It is always worth dumping your results step by step to see what you get so that you know how to fetch it, e.g.

<?php dump($painting->blueprint()->field('size')['options']) ?>

will give you an array like that:

Array
(
    [w30h40] => 30 × 40 cm
    [w40h30] => 40 × 30 cm
    [w60h80] => 60 × 80 cm
    [w80h60] => 80 × 60 cm
)

Then you see that there is no text index, but you can fetch the individual values by their keys.

1 Like

Thank you, it works very well now.

Just to understand: for instance, the value is w30h40 and the key is 30 × 40 cm. Right?

In the array

Array
(
    [w30h40] => 30 × 40 cm
    [w40h30] => 40 × 30 cm
    [w60h80] => 60 × 80 cm
    [w80h60] => 80 × 60 cm
)

w30h40 is the array key, and 30 × 40 cm is the value.

You get value of an array, by addressing its index:

$array = [
  'w30h40' => '30 × 40 cm',
  'w40h30' => '40 × 30 cm'
  // ...
];

echo $array['w30h40']; // will output '30 x 40 cm'
1 Like

Hi Sonja, I have a similar problem, but I want to access the options with the query language for a files info text in a blueprint:
info: “{{ painting.blueprint.field(‘size’).options.w30h40 }}”

This works, but I would like to access the option with the selected “painting.size”, but I can’t get it working…
info: “{{ painting.blueprint.field(‘size’).options[painting.size] }}”

thanks for your help :slightly_smiling_face:
regards andy

You would have to create a custom method for that, there are limitations to what you can do with the query language.

I’m a bit surprised this works, because painting is nothing Kirby would recognize?