# Test for empty Object field

**URL:** <https://forum.getkirby.com/t/test-for-empty-object-field/28879>\
**Category:** Questions\
**Tags:** v3\
**Created:** [June 22, 2023, 12:27pm UTC](https://forum.getkirby.com/t/test-for-empty-object-field/28879 "2023-06-22T12:27:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![coko](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@coko](https://forum.getkirby.com/u/coko)\
**Post date:** [June 22, 2023, 12:27pm UTC](https://forum.getkirby.com/t/test-for-empty-object-field/28879/1 "2023-06-22T12:27:23Z")

</div>

The [how to use in templates code](https://getkirby.com/docs/reference/panel/fields/object#how-to-use-in-templates-snippets) for the Object field gives me the impression that testing for an empty object can be done with an if statement?

```auto
<?php if ($contact = $page->contact()->toObject()): ?>
<dl>
  <dt>Photo</dt>
  <dd><?= $contact->photo()?->toFile()->crop(200) ?></dd>
  <dt>Name</dt>
  <dd><?= $contact->name() ?></dd>
  <dt>Email</dt>
  <dd><?= $contact->email() ?></dd>
  <dt>Phone</dt>
  <dd><?= $contact->phone() ?></dd>
</dl>
<?php endif ?>

```

This still gives me an empty Description List.  
How should I test for an empty object?

---

<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:** [June 22, 2023, 12:32pm UTC](https://forum.getkirby.com/t/test-for-empty-object-field/28879/2 "2023-06-22T12:32:04Z")

</div>

The `toObject()` field method returns a Content object: [$field-\>toObject() | Kirby CMS](https://getkirby.com/docs/reference/templates/field-methods/to-object#returns)

You would then have to check if `$contact->data()` returns an empty array.

---

<div class="post-metadata">

**Author:** ![coko](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@coko](https://forum.getkirby.com/u/coko)\
**Post date:** [June 22, 2023, 12:41pm UTC](https://forum.getkirby.com/t/test-for-empty-object-field/28879/3 "2023-06-22T12:41:24Z")

</div>

Ok, so this works:

```auto
<?php $contact = $page->contact()->toObject();
   if (!empty($contact->data())):
?>
<dl>
  <dt>Photo</dt>
  <dd><?= $contact->photo()?->toFile()->crop(200) ?></dd>
  <dt>Name</dt>
  <dd><?= $contact->name() ?></dd>
  <dt>Email</dt>
  <dd><?= $contact->email() ?></dd>
  <dt>Phone</dt>
  <dd><?= $contact->phone() ?></dd>
</dl>
<?php endif ?>

```

Do you approve?

---

<div class="post-metadata">

**Author:** ![rasteiner](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rasteiner/32/4788_2.png) [@rasteiner](https://forum.getkirby.com/u/rasteiner)\
**Post date:** [June 22, 2023, 1:01pm UTC](https://forum.getkirby.com/t/test-for-empty-object-field/28879/4 "2023-06-22T13:01:10Z")

</div>

> [@coko](#):
>
> ```auto
> <?php $contact = $page->contact()->toObject();
> if (!empty($contact->data())):
> ?>
> 
> ```

if you prefer you can also write it on one line:

```php
<?php if(($contact = $page->contact()->toObject())->data()): ?>

```

---

<div class="post-metadata">

**Author:** ![coko](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@coko](https://forum.getkirby.com/u/coko)\
**Post date:** [June 22, 2023, 1:09pm UTC](https://forum.getkirby.com/t/test-for-empty-object-field/28879/5 "2023-06-22T13:09:48Z")

</div>

For the object I’m using this will throw an error:  
`Call to a member function date() on array`

I guess in that case I have to loop over the data?  
I’ll stick to the extra line in that case.

---

<div class="post-metadata">

**Author:** ![rasteiner](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/rasteiner/32/4788_2.png) [@rasteiner](https://forum.getkirby.com/u/rasteiner)\
**Post date:** [June 22, 2023, 1:30pm UTC](https://forum.getkirby.com/t/test-for-empty-object-field/28879/6 "2023-06-22T13:30:04Z")

</div>

sounds like you missed a pair of parentheses around the assignment to `$contact`, but I guess that if that happens it’s because it wasn’t very readable 🙂

So sticking to the 2 lines version is still desirable

---

<div class="post-metadata">

**Author:** ![coko](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@coko](https://forum.getkirby.com/u/coko)\
**Post date:** [June 22, 2023, 1:33pm UTC](https://forum.getkirby.com/t/test-for-empty-object-field/28879/7 "2023-06-22T13:33:45Z")

</div>

Yes, I did miss that, this will get me every time I’m sure.  
Sticking to two lines for a readable solution.  
Thanks
