Check if items isNotEmpty in structure field with toLinkObject

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.

Bildschirmfoto 2022-07-04 um 18.40.14

Could you share the error message please (and when asking about a plugin, a link to the plugin is usually useful as well).

Error thrown with message “Call to a member function title() on null”

PS: I guess title()is wrong

This is the YML:

      introFotos:
        label: Intro Fotos
        type: structure
        columns:
            fotos:
                width: 1/2
            link:
                 width: 1/2

        fields:
            fotos:
                width: 1/2
                type: files

            link:
                width: 1/2
                type: link
                label: Link
                linkTypes:
                    - page

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.

<?= ($p = $item->link()->toLinkObject()) ? $p->title() : 'Some fallback text'; ?>

Or no figcaption if page doesn’t exist/none selected:

<?php if ($p = $item->link()->toLinkObject()): ?>
    <figcaption><?= $p->title(); ?></figcaption>
<?php endif ?>

Ah, because I have no field for the figcaption “isEmpty” is not the correct check?

I’m gonna get a huge mug of coffee and check (and bookmark) that OOP page.

Thank you $again

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