# Collecting user infos on a page + asking if the fields are empty

**URL:** <https://forum.getkirby.com/t/collecting-user-infos-on-a-page-asking-if-the-fields-are-empty/25178>\
**Category:** Questions\
**Tags:** v3\
**Created:** [April 4, 2022, 4:43pm UTC](https://forum.getkirby.com/t/collecting-user-infos-on-a-page-asking-if-the-fields-are-empty/25178 "2022-04-04T16:43:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![exeldoc](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/exeldoc/32/6224_2.png) [@exeldoc](https://forum.getkirby.com/u/exeldoc)\
**Post date:** [April 4, 2022, 4:43pm UTC](https://forum.getkirby.com/t/collecting-user-infos-on-a-page-asking-if-the-fields-are-empty/25178/1 "2022-04-04T16:43:33Z")

</div>

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; ?>

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 4, 2022, 4:59pm UTC](https://forum.getkirby.com/t/collecting-user-infos-on-a-page-asking-if-the-fields-are-empty/25178/2 "2022-04-04T16:59:50Z")

</div>

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

```auto
<?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.

---

<div class="post-metadata">

**Author:** ![exeldoc](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/exeldoc/32/6224_2.png) [@exeldoc](https://forum.getkirby.com/u/exeldoc)\
**Post date:** [April 4, 2022, 6:09pm UTC](https://forum.getkirby.com/t/collecting-user-infos-on-a-page-asking-if-the-fields-are-empty/25178/3 "2022-04-04T18:09:09Z")

</div>

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:

```auto
<?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.

> [@texnixe](#):
>
> 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.

What is the difference here? field or just fetching?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 4, 2022, 6:44pm UTC](https://forum.getkirby.com/t/collecting-user-infos-on-a-page-asking-if-the-fields-are-empty/25178/4 "2022-04-04T18:44:06Z")

</div>

> [@exeldoc](#):
>
> `<?php if ($image = $user->image()->isNotEmpty()) : ?>`

Ok, that’s why I was asking. If it’s a field, you have to convert it to a file object:

```auto
<?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; ?>

```

---

<div class="post-metadata">

**Author:** ![exeldoc](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/exeldoc/32/6224_2.png) [@exeldoc](https://forum.getkirby.com/u/exeldoc)\
**Post date:** [April 4, 2022, 9:44pm UTC](https://forum.getkirby.com/t/collecting-user-infos-on-a-page-asking-if-the-fields-are-empty/25178/5 "2022-04-04T21:44:31Z")

</div>

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. 🥰

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 5, 2022, 6:23am UTC](https://forum.getkirby.com/t/collecting-user-infos-on-a-page-asking-if-the-fields-are-empty/25178/6 "2022-04-05T06:23:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![exeldoc](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/exeldoc/32/6224_2.png) [@exeldoc](https://forum.getkirby.com/u/exeldoc)\
**Post date:** [April 5, 2022, 8:09am UTC](https://forum.getkirby.com/t/collecting-user-infos-on-a-page-asking-if-the-fields-are-empty/25178/7 "2022-04-05T08:09:48Z")

</div>

OK. I see.  
Thanks very much for explaining.
