Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Show CMS field only on top level pages

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

Page: 1
Go to End
Author Topic: 1452 Views
  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Show CMS field only on top level pages Link to this post

    HI, I have a site where each top level page has data associated which controls pages under it (styles etc). I'm looking for a way to specify a field in Page.php but only have it show up in the CMS if it is a top level page.....any ideas out here?

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: Show CMS field only on top level pages Link to this post

    Well you could have a condition in the getCMSFields - if($this->ParentID == 0) since if its a top level page, it has no parent .

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: Show CMS field only on top level pages Link to this post

    Great thanks Will.....

    the other issue Im having with this is retreiving an Image from the top level. ie I have the following function in Page_Controller:

    function getSubNavBg() {
       return $this->Level(1)->SectionNavImg;   
       }

    where SectionNavImg is an Image

    and in my template I'm trying to set a background image on a sub nav for a section using eg:

    <style>
    ul#SubNav {background:url($getSubNavBg.Filename) }
    </style>

    but it will not bring the filename through....am I missing something blindingly obvious?

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: Show CMS field only on top level pages Link to this post

    Try

    ul#SubNav {background:url({$SubNavBg.Filename}) }

    If that doesn't work make sure $SubNavBg exists. Try a <% debug SubNavBg %> and see what it says.

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: Show CMS field only on top level pages Link to this post

    Hi, doesn't seem to be any JOy here...I'll list all the relevant code in case we've missed something:

    Page.php

    class Page extends SiteTree {
       
       public static $db = array(
        'SectionColour' => 'Text',
        'SectionHighlightColour' => 'Text'
       );   
       public static $has_one = array(
       'SectionNavImg' => 'Image'
       );   
       
       function getCMSFields() {
          $fields = parent::getCMSFields();      
          
          if($this->ParentID == 0){
          $fields->addFieldToTab('Root.Content.Theme', new ColorField('SectionColour', 'Section Colour'));
          $fields->addFieldToTab('Root.Content.Theme', new ColorField('SectionHighlightColour', 'Section Highlight Colour'));      
          $fields->addFieldToTab('Root.Content.Theme', new ImageField('SectionNavImg'));
          }      
          return $fields;
       }
       
    }

    class Page_Controller extends ContentController {
       
       public function init() {
          parent::init();
       }
       
       
       function SectionColour() {
       return $this->Level(1)->SectionColour;   
       }
       
       function HiglightColour() {
       return $this->Level(1)->SectionHighlightColour;   
       }
       
       function SubNavBg() {
       return $this->Level(1)->SectionNavImg;   
       }   
    }

    In my template $SectionColour returns the correct value from the top level page, but the Image ( using $SubNavBg.Filename ) will just not come through.......

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: Show CMS field only on top level pages Link to this post

    $this->Level(1)->SectionNavImg

    Probably because the image is a relationship you need to return the object so try either

    return $this->Level(1)->SectionNavImg();

    or

    return $this->Level(1)->dbObject('SectionNavImg');

    The first one should work though.

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: Show CMS field only on top level pages Link to this post

    Great thanks Will!
    return $this->Level(1)->SectionNavImg(); works

    knew it would be something simple

    1452 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.