coko
1
The how to use in templates code for the Object field gives me the impression that testing for an empty object can be done with an if statement?
<?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?
1 Like
The toObject() field method returns a Content object: $field->toObject() | Kirby CMS
You would then have to check if $contact->data() returns an empty array.
coko
3
Ok, so this works:
<?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?
if you prefer you can also write it on one line:
<?php if(($contact = $page->contact()->toObject())->data()): ?>
coko
5
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.
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
coko
7
Yes, I did miss that, this will get me every time I’m sure.
Sticking to two lines for a readable solution.
Thanks