Structure get/find/request entry by keyname

Hey - maybe I’m blind, but I didn’t find a build-in-solution in the docs or in this forum to a get a specific entry from a structure field. For example I use the structure field to create a dictionary like:

// blueprints/site.yml 

dictionary:
   type: structure
   fields:
      key:
        type: text
      value:
        type: text

Now, in the templates I would like to have a method to get a specific entry by key like this:

$site->dictionary()->toStructure()->findEntryByKey("[A UNIQUE KEY]")->value()

I tried it withtoStructure()->get() and toStructure()->filterBy() but without success.

One more important question: Are the word key and value reserved by Kirby, or can this words be used like in the example above?

Thanks for your help

I think it will work in the example below:

if ($row = $site->dictionary()->toStructure()->findBy("key", "something")) {
    echo $row->value();
}

About reserved method names:

1 Like

Thanks - this works perfectly.

In the meantime I found another solution I like to share. Maybe it is working out for someone too.
I converted the indexed array from the structure field (via ->yaml()) into an associated array. After that, i got the shortest way to access a value by a key:

$associativeDictionary= [];
foreach ($site->dictionary()->yaml() as $row) {
    $associativeDictionary[$row['key']] = $row["value"];
}
$valueByKey = $associativeDictionary["some-key"]

I stored $associativeDictionary in the controller/site.php and have easy access to all entries everywhere – including “nice” errors, if a key is not defined :wink: