Hey, I can’t get this running.
I guess I have to put ->toUser()
somewhere different.
Right now it is just not loading the images.
<?php if ($user = $kirby->users()): ?>
<?php foreach ($user->sortBy('lastname', 'asc', 'firstname', 'asc') as $user): ?>
<li><?= $user->firstname()?> <?= $user->lastname()?></li>
<?php if ($image = $user->toUser()->image()->isNotEmpty()): ?>
<div class="imageContainer">
<div class="artistPortrait" style="background-image: url(<?= $image->image()->url() ?>)">
</div>
</div>
<?php endif; ?>
<?php endforeach ?>
<?php endif; ?>
There are a few issues with your code:
- wrapping ul element missing
- li element should wrap the div as well
- don’t use same variable for plural users and user
- you cannot call
toUser
on a user object
- if ($user = $kirby->users()) doesn’t make sense because it’s always true
<?php $users = $kirby->users()->sortBy('lastname', 'asc', 'firstname', 'asc') ?>
<?php if ($users->isNotEmpty()): ?>
<ul>
<?php foreach ($users as $user) : ?>
<li><?= $user->firstname() ?> <?= $user->lastname() ?>
<?php if ($image = $user->image()) : ?>
<div class="imageContainer">
<div class="artistPortrait" style="background-image: url(<?= $image->image()->url() ?>)">
</div>
</div>
<?php endif; ?>
</li>
<?php endforeach ?>
</ul>
<?php endif ?>
If image
in $user->image()
is supposed to be a field (rather than fetching just an image from the user), the code needs to be adapted a bit.
Thanks for responding
Sorry about the missing HTML tags. I tried to simplify and screwed up.
But it is not about checking if the user is empty.
There is a files
field in the user blueprint for uploading a portait. If the user doesn’t do that, it should appear a fallback.
<?php if ($image = $user->image()->isNotEmpty()): ?>
<div class="imageContainer">
<div class="artistPortrait" style="background-image: url(<?= $image->url() ?>)">
</div>
</div>
<?php endif; ?>
<?php if ($image = $user->image()->isEmpty()): ?>
<div class="artist-no-image">
</div>
<?php endif; ?>
I thought that could do the job:
<?php if ($image = $user->image()->isNotEmpty()) : ?>
But then it say: isNotEmpty on null
Looks like is can’t ask if a files
field from the user blueprint is empty or not.
What is the difference here? field or just fetching?
Ok, that’s why I was asking. If it’s a field, you have to convert it to a file object:
<?php if ($image = $user->content()->get('image')->toFile()) : ?>
<div class="imageContainer">
<div class="artistPortrait" style="background-image: url(<?= $image->url() ?>)">
</div>
</div>
<?php else : ?>
<div class="artist-no-image">
</div>
<?php endif; ?>
It works, though I don’t understand why…
I mean, e.g. a text field works as expacted.
<?php if ($user->website()->isNotEmpty()): ?>
<a href="<?= $user->website() ?>" class="button">website</a>
<?php endif ?>
Why the detour for files?
Thanks anyway, I would have never got to this solution.
Checking if a text field is empty makes sense, because it returns if the field has a value or not.
In the case of files, it has no value to know if the field is empty or not. Because even if the field contains a value, it doesn’t mean that the file that was selected still exists at the time you are requesting it. Also, to get a file object from the string stored in your field, you need to call the toFile()
method.
The toFile()
method (or toFiles()
for multiple files) checks if a file with the given path exists in your project.
1 Like
OK. I see.
Thanks very much for explaining.