Get all options from select field

hey everyone!

does someone know if there is a way to get all options from a select field in the template file? (not just the chosen one)

thank you very much in advance!

  • sigi

You can get all options via the $page->blueprint() method, see also this cookbook recipe:

hey @texnixe, thank you for the answer!

i cannot seem to get it to work, maybe you see my mistake:

<?php foreach($page->blueprint()->fields()['category']->options() as $item) : ?>
  <option value="<?= $item->value() ?>"><?= $item->text() ?></option>
<?php endforeach ?>

it gives me the error: Call to a member function options() on array

As the error message says, $page->blueprint()->fields()['category'] returns an array, so you cannot use -> a class method, but have to fetch the options via the array index.

Also, we want to make sure that this index exists before we continue:

$options $page->blueprint()->field('category')['options'] ?? null;
if ( $options ) : ?>
  <?php foreach($options as $key => $item) : ?>
    <option value="<?= $key ?>"><?= $item ?></option>
  <?php endforeach ?>
<?php endif ?>

as always, thank you for your help!