Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Banner Image defaults to parent


Go to End


2309 Views

Avatar
NickJacobs

Community Member, 148 Posts

20 March 2009 at 5:37pm

Edited: 20/03/2009 5:41pm

In a site I'm building I want different banner images for each section of the site, and so want a page to default it's parents banner if one is not set. I have set up the following code, but I don't see anything on the page

the code is based on this archived post

In Page.php

class Page extends SiteTree {
	
	static $has_one = array(
	'BannerImage' => 'Image'
   );
.....



class Page_Controller extends ContentController {
	
	.....	
	
	
function getBannerImage() {       
             
      if(empty($BannerImage->ID)){         
         $BannerImage = $this->getParentBannerImage($this);              
      	}       
      return $BannerImage; 
   } 
    
   
   private function getParentBannerImage($CurrentPage){       
      $Parent = $CurrentPage->Parent;       
       
      if(is_object($Parent)){ 
                   
         if(!empty($BannerImage->ID)){ 
            return $BannerImage; 
            // We found the current header! Let's return it! 
         } else { 
            return $this->getParentBannerImage($Parent); 
            // We didn't find a header,so we have to call ourselves again 
         } 
          
      } else { 
         return false; 
      } 
   }
	
....

In Page.ss i'll just do something like this:

<div id="banner" style="background:url(<% control getBannerImage %>$URL<% end_control %>)"></div> 

So, it's probably something completely obvious...just not to me at the mo!