I use the Kirby-Link-Field plugin in a structure field.
I get an error message if I do this:
<?php
// using the `toStructure()` method, we create a structure collection
$items = $page->introFotos()->toStructure();
// check if the field is not empty
// we can then loop through the entries and render the individual fields
foreach ($items as $item): ?>
<?php if ($items->isNotEmpty()) : ?>
<?php foreach ($item->fotos()->toFiles() as $image): ?>
<a class="image-link" href="<?= $item->link()->toLinkObject(); ?>">
<figure class="hover-effect-figure">
<img src="<?= $image->crop(500)->url() ?>">
<figcaption><?= $item->link()->toLinkObject()->title(); ?></figcaption>
</figure>
</a>
<?php endforeach ?>
<?php endif ?>
<?php endforeach ?>
It seems to something wrong with the figcaption thing, because that is highlighted in the error message.
I want to give out the name aka title() of the page where the link links to. That works well, but not if the user forgets to add a link in that structure field.
Well, the usual problem, missing check for existing page/object. When you want to call a class method like title(), url() or whatever, you need an instance of that class. You cannot call such a method on null, false, another object of another class etc.
isEmpty()/isNotEmpty() is useful for contexts where you only want to output something if a field is not empty, to prevent that you end up with empty HTML, for example:
<?php if ($page->subheadline()->isNotEmpty()): ?>
<h3><?= $page->subheadline()->html() ?></h3>
<?php endif; ?>
Without the if statement here, you could end up with empty h3 tags.
Where pages/files etc are concerned, you can of course check if the field is empty or not, but that’s only half the battle. Because if the field is not empty, it still doesn’t mean that your object exists (for example, you might have deleted the reference page/file/user or renamed it).