Page added to menu that later is deleted results in error

I’ve followed this tutorial to build menus: https://getkirby.com/docs/cookbook/templating/menu-builder

If I add a page to a menu and later delete that page, there’s then an empty row:

And I get the following error:
Call to a member function url() on null

Here’s a snippet from my code:

<?php if ($footerMenuLink->link()->value() === 'page' && $footerMenuLink->page()->isNotEmpty()): ?>
  <li><a href="<?= $footerMenuLink->page()->toPage()->url() ?>"><?= $footerMenuLink->page()->toPage()->title() ?></a></li>
<?php endif ?>

Using isNotEmpty is not enough. I’ve tried to play with $page->exists() without success.

It would help to know the complete code where you define $footerMenuLink and the blueprint structure…

Blueprint:

footerMenus:
  columns:
    title:
      width: 1/1
  label: Menus
  max: 4
  translate: false
  type: structure
  fields:
    title:
      label: Title
      type: text
    links:
      columns:
        page:
          width: 1/2
        url:
          width: 1/2
      label: Links
      type: structure
      fields:
        link:
          default: page
          label: Link
          options:
            page: Page
            url: URL
          required: true
          type: select
          width: 1/4
        page:
          label: Page
          max: 1
          type: pages
          when:
            link: page
          width: 3/4
        label:
          label: Label
          type: text
          when:
            link: url
          width: 1/4
        url:
          label: URL
          type: url
          when:
            link: url
          width: 1/4
        target:
          label:
          text:
            - Open in new tab
            - Open in new tab
          type: toggle
          when:
            link: url
          width: 1/4

Snippet:

<?php if ($footerMenus->first()): ?>

  <?php foreach ($footerMenus as $footerMenu):

    $footerMenuLinks = $footerMenu->links()->toStructure();

  ?>

    <?php if ($footerMenuLinks->first()): ?>
      <ul>

        <?php foreach ($footerMenuLinks as $footerMenuLink): ?>

          <?php if ($footerMenuLink->link()->value() === 'page' && $footerMenuLink->page()->isNotEmpty()): ?>
            <li><a href="<?= $footerMenuLink->page()->toPage()->url() ?>"><?= $footerMenuLink->page()->toPage()->title() ?></a></li>
          <?php elseif ($footerMenuLink->link()->value() === 'url' && $footerMenuLink->label()->isNotEmpty() && $footerMenuLink->url()->isNotEmpty()): ?>
            <li><a href="<?= $footerMenuLink->url() ?>"<?php if ($footerMenuLink->target()->bool()): ?> target="_blank"<?php endif ?>><?= $footerMenuLink->label() ?></a></li>
          <?php endif ?>

        <?php endforeach ?>

      </ul>
    <?php endif ?>

  <?php endforeach ?>

<?php endif ?>

This should be

if ($footerMenuLinks->isNotEmpty()): ?>

And here you have to check if the page exists before you call the title() method.

I can’t repeat it often enough: Never call a class method without making sure you have an instance of that class.

1 Like

Thanks made that change.

I’ve added && $footerMenuLink->page()->toPage()->exists() but still getting the error.

<?php if ($footerMenuLink->link()->value() === 'page' && $footerMenuLink->page()->isNotEmpty() && $footerMenuLink->page()->toPage()->exists()): ?>
  <li><a href="<?= $footerMenuLink->page()->toPage()->url() ?>"><?= $footerMenuLink->page()->toPage()->title() ?></a></li>
<?php elseif ($footerMenuLink->link()->value() === 'url' && $footerMenuLink->label()->isNotEmpty() && $footerMenuLink->url()->isNotEmpty()): ?>
  <li><a href="<?= $footerMenuLink->url() ?>"<?php if ($footerMenuLink->target()->bool()): ?> target="_blank"<?php endif ?>><?= $footerMenuLink->label() ?></a></li>
<?php endif ?>

Try AND. && and AND do behave different in PHP.

Same, getting:
Call to a member function exists() on null

You can’t use exists(), to check if a page exists, because that method already requires a page object, which is what you want to check.

<?php if ($footerMenuLink->page()->toPage()): ?>
<?php endif ?>
1 Like

Ah! Thanks!!