Calling a users profile image, from a user select

Hello,

Forgive my beginner PHP knowledge, but I can’t seem to find an article specifically for this problem. In the blueprint file for a project, I have included a user selectbox:

author:
  label: Author
  type: user

This gives me the following:

All user have profile images and I want to return them as seen in this mock up:

I know from this link, https://getkirby.com/docs/cheatsheet/user/avatar I call a users avatar, but you need to know the username, in the case ‘bastian’:

<?php if($avatar = $site->user('bastian')->avatar()): ?>
<img src="<?php echo $avatar->url() ?>" alt="Bastian's avatar">
<?php endif ?>

I know I can call the username with the following:

<?= $project->author()->html() ?>

But when I mix the two together I get an error. Can anyone help?


Also, you can see from the screenshot above I’ve used multiple select boxes, for example in the “Division” and “Client” categories. Say I have the following in the parameters file:

  client:
    label: Client
    type: select
    options:
      ben: Ben Sherman
      ted: Ted Baker

And I use the same call:

<?= $project->client()->html() ?>

this would return “ben” or “ted”. Not “Ben Sherman” or “Ted Baker”, which is what i want.
What would I need to use to call the value rather than the option?

Thanks in advance for the help, Rob

Hi Rob,

for the first problem: you can pass the field value to the user method:

<?php if($avatar = $site->user($project->author()->value())->avatar()): ?>
<img src="<?php echo $avatar->url() ?>" alt="Bastian's avatar">
<?php endif ?>

For the second:

<?php
$user = $project->client()->value();
$firstname = $site->user($user)->firstname();
$lastname = $site->user($user)->lastname();

$clientName = $firstname . ' ' . $lastname;
1 Like

Also, if the select fields you want to get the label from aren’t specifically filled with usernames (sounds like at least ‘Division’ isn’t), take a look at this plugin -> https://github.com/AugustMiller/kirby-architect

1 Like

If you don’t want to use the plugin, you can also use a category map (for the divisions).

1 Like

Overwhelmed by the responses, thank you for the help.
You code worked! Loving Kirby right now :slight_smile: Thank you!