Get value from select field that's in a structure field

Okay. I give up. How do i get the value from a select field that’s in a structure field. Consider the following blueprint. Notice that my ‘social_icon’ select field lists the images on the page.

  social:
    label: Social Channels
    type: structure
    style: table
    entry: >
      {{social_name}}
    fields:
      social_name:
        label: Name
        type: text
      social_icon
        label: Icon (SVG)
        type: select
        options: images
      social_url:
        label: URL
        type: text

I used a foreach loop and iterated through the “rows”. this allows me to easily get the text field values, but i’m struggling with the select value - maybe because i’m not sure how Kirby is outputting that value. Any insight would be appreciated.

Hey there,

it’s fairly easy to get a value of a specific field in a structure. But I think you already did this correctly by iterating through the fields as you mentioned. The select field with “images” as options simply outputs the filename of the selected image. So you will have to search the file in the current page.

<?php
    foreach($page->social()->toStructure() as $social):
?>
    <a href="<?= $social->social_url() ?>">
        <span class="name"><?= $social->social_name() ?></span>
        <img src="<?= $page->image($social->social_icon())->url() ?>"/>
    </a>
<?php
    endforeach;
?>

It’s untested but it should work.

Cheers,
Thomas

1 Like

Thanks for the quick response. That doesn’t work for me. There’s one more item worth mentioning; these fields are being set in the site blueprint. The following code echos each value except that blasted select field value.

$rows = $site->social()->toStructure();
        if(count($rows)):
        foreach($rows as $row):
            echo '<p>';
            echo 'social_url: ' . $row->social_url() . '<br/>';
            echo 'social_name: ' . $row->social_name() . '<br/>';
            echo 'social_icon: ' . $page->image($row->social_icon())->url();
            echo '</p>';
        endforeach;
        endif;
1 Like

You write

$site->social()->toStructure()

instead of

$page->social()->toStructure()

So does that mean the “social” structure is in the site settings? If so, then you will have to change the social_icon echo accordingly:

echo 'social_icon: ' . $site->image($row->social_icon())->url();
1 Like

I figured that and did try $site instead of $page. For example, the following geneates all values except that select value.

$rows = $site->social()->toStructure();
if(count($rows)):
foreach($rows as $row):
    echo '<p>';
    echo 'social_url: ' . $row->social_url() . '<br/>';
    echo 'social_name: ' . $row->social_name() . '<br/>';
    echo 'social_icon: ' . $site->image($row->social_icon())->url();
    echo '</p>';
endforeach;
endif;

I quickly reconstructed your setting and files on a local, empty install.

Your blueprint is missing a “:” after “social_icon” :slightly_smiling:

Oh dear. Yep. That’ll jack things up. It works now. I appreciate your time. Thank you.