Fetching values from a structure field in a multiselect field

When fetching options from a structure field in a multiselect field like documented here: https://getkirby.com/docs/reference/panel/fields/multiselect#options-from-other-fields__options-from-structure-field, what is the best way to access all values and use them seperately in a template?

For instance, I have a structure field on the page „Events“ with these (or more) fields:

type: fields
fields:
  events:
    label: Events
    type: structure
    fields:
      eventtitle:
        label: Title
        type: text
      eventdate:
        label: Date
        type: date
      eventspeaker:
        label: Speaker
        type: text
      eventtype:
        label: Type
        type: text

And the multiselect field on a different page (or the Editor / Builder) looks like this:

  eventlist:
    type: multiselect
    label: Choose events to display
    options: query
    query:
      fetch: site.find('Events').events.toStructure
      text: '{{ structureItem.eventtitle }}'
      value: '{{ structureItem.eventtitle }}'

And the template looks something like this:

<ul class="eventlist">
  <?php foreach ($page->eventlist()->split() as $event): ?>
  <li><?= $event ?></li>
  <?php endforeach ?>
</ul>

Now I get a list of the selected event titles, which works fine. But whats the best way to access all values seperately? Of course I could store all values like this:

value: '{{ structureItem.eventtitle }} | {{ structureItem.eventdate }} ... '

and then Str::split them in the template, but I guess that’s not very elegant :wink: , especially with more complex values.

Thanks for help!

Assuming that the eventtitle is unique for these structure field, you can find the structure item by title and thus access all fields of this item.

$events = page('somepage')->events()->toStructure();
$event = $events->findBy('eventtitle', $event); // $event is the title

if ($event) {
echo $event->eventtitle();
echo $event->eventspeaker();
// etc.

In this case the titles are unique. Good idea, thanks!