Is it possible to get custom page attributes from helper file?

Permalink 1 user found helpful
I have some page templates (in a 5.6 install) that use the below snippet to change a header background image using inline css with a custom page image attribute if it is set.

if ($file = $c->getAttribute('header_image')) {
   $url = "'".$file->getVersion()->getDownloadURL()."'";
   echo 'style="background-image: url('.$url.');"';
}


I was looking to move this snippet to a function in a helper file and then call it into my templates where required using something like
echo Loader::helper('myhelper')->headerImage();

Purely with a view to neatening things up :-)

However I can't get this to work, I get an error
"Fatal error: Call to a member function getCollectionAttributeValue() on null in..[helper file]"

Is what I'm trying to do not possible, can I not get a page attribute outside the page because it's not yet fully initialised or something similar?

I know I've got the helper file set up right as if I can change the code to just echo out a test message into the page and this works fine.

mrjcgoodwin
 
enlil replied on at Permalink Best Answer Reply
enlil
try passing the value of $c into the function like:
echo Loader::helper('myhelper')->headerImage($c);


then where you declare the function in your helper:
function headerImage($c) {...
mrjcgoodwin replied on at Permalink Reply
mrjcgoodwin
Awesome, absolutely spot on thanks - such an obvious mistake when someone else points it out to you! :P