Sending data to a mail template: user field is array, even if multiple is set to false

Hey there.

I have an issue continuing this question:

I want to transfer data to a mail template like this:

kirby()->email([
   'template' => 'new-block',
   'from' => 'mail@address.de',
   'to' => $newwork->nextartist()->toUser()->email(),
   'subject' => 'Answer now!',
   'data' => [
     'name' => $newwork->author()->toUser()->firstname(),
     ]
  ]);

It is about name.

In the blueprint I set the user field muliple: false.

author:
type: users
multiple: false
width: 1/2

The mail that arrives just shows “array” and not the field content of firstname of the one logged in selected user.

How do I change the array of the user field into the one user?

What have you put into your email template?

$newwork->author()->toUser()->firstname(),

Should only return a field object, not a string, so should be

$newwork->author()->toUser()->firstname()->value(),

but you would be well advised to check for a user object first.

'name' => ($user = $newwork->author()->toUser()) ? $user->firstname()->value() : '',
1 Like

This was the part in my email template:

<?= $name ?>

I made progress myself – but your way looks cleaner.
Here is how I did it:

'name' => $newwork->author()->first()->toUser()->firstname(),

Because I figured out, that the arrays’ length was just 1 – the default logged in author…

Thanks for your help.

That doesn’t make sense…? You cannot call first() on a field object.

Mh. It worked somehow, any way. But I will go with your solution.