All pages work except one - controller and template are the same

Hello there,

I am encountering a weird thing on my project. All pages work ok for except one. All of them use the same template and the same controller. I deleted the page an created a new one, but the curse stays with it.
Has somebody encountered something like that? where and how could I find the problem?

This is the controller:

   <?php

return function ($page, $pages, $site, $kirby){
  #Grab the data from the default controller
  $shared = $kirby->controller('site', compact('page', 'pages', 'site', 'kirby'));

  #Work on the page specific logic
  if($page->heroimage()->exists()){
    $heroimage = $page->heroimage()->toFile();
  } else {
    $heroimage = $page->image();
  }

  #Create objects for the project navigations
  $next = $page->next() ; 
  $prev = $page->prev() ;

  #Create sections on page
  $datum = $page->datum();
  $client = $page->client();
  $details = $page->details();
  $text = $page->text();
  $documentation = $page->documentation();

  return a::merge($shared , compact('heroimage', 'prev', 'next', 'datum', 'client', 'details', 'text', 'documentation'));
};

And the there is the passage where an Error - Call to a member function url() on null - is thrown:

<div class="project-images">
      <div class="project-heroimage">
        <figure>
          <a href="<?= $heroimage->url() ?>"> 
            <img 
              class="gallery"
              src=" <?= $heroimage->resize(1200, 1200)->url() ?>" 
              alt=" <?= $heroimage->alt() ?>" 
            >
          </a>
        </figure>
      </div> 

Any hint is very much appreciated!

You always have to check if an object of the given class exists before you call a class method like url().

Some background information:

1 Like

Thank you very much for the response. I’m checking in the controller on it is existing via:
…and there is a picture in the page-folder!

  if($page->heroimage()->exists()){
    $heroimage = $page->heroimage()->toFile();
  } else {
    $heroimage = $page->image();
  }

Also following your advice I wrapped call in an if clause to check again…

<?php if($heroimage): ?>
        <figure>
          <a href="<?= $heroimage->url() ?>"> 
            <img 
              class="gallery"
              src=" <?= $heroimage->resize(1200, 1200)->url() ?>" 
              alt=" <?= $heroimage->alt() ?>" 
            >
          </a>
        </figure>
        <?php endif ?>

and indeed - the page is running. However, there is no picture displayed, although there is one in the folder…

(And yes, I should dig deeper and read the recommended information…)

This checks if the field exist, but that doesn’t mean it contains any content and even if is did, that doesn’t mean that the image exists.

What do you see when you inspect the code in your browser’s inspector?

1 Like

Thank you so much! :sweat_smile:

That’s it! The problem was that I only checked on the field and not wether it was empty or not.
$page->heroimage()->isNotEmpty() was the key. I understood now that any new created page contains the field which is empty if nothing is selected - but it nevertheless exists!

what an eye-opener.

thank you very much!