File attribute error ONLY on Edit Page Defaults

Permalink
Hey there, this is my first ever post here on the community. Usually I find all the answers just by searching here or simply reading the Documentation, so I never needed to ask anything.

I'm building a site where I use a custom attribute to display an image as full page background, and I want it hardcoded into the page template so I used this code:

echo '<img src="'.$page->getAttribute('bgimage')->getRelativePath().'">';


The image displays with no problem on every page, EXCEPT when I want to edit Page Type Defaults.

There, I get an error saying "Call to a member function getRelativePath() on a non-object"

I need help, I can't figure out how to overcome this.

 
glockops replied on at Permalink Best Answer Reply
glockops
I imagine your custom template has some code to define $page. That code is not being properly executed when working with page type defaults.

If that is the case, continue reading.
I had a similar issue when inheriting attributes and styles from a parent page, since page type defaults don't have parents. What you can do is specifically set $page to a value if the page type defaults are being loaded.

if(!$c->isMasterCollection()) {
            $page = ...  // Put your code here.
        } else {
            $page = Page::getByID('1'); // This will return the homepage and you can use that to load the bgimage that you specified on the homepage, which should be good enough for your page type defaults.
        }


Alternatively, you could check to see if $page was set, and if not them skip the entire echo. But you may have other things that rely on $page.

Hope that helps!
gcadiz replied on at Permalink Reply
Hey thanks a lot, that worked!

At least, now I can load a message when editing defaults.