Jump to:

22972 Posts in 11596 Topics by 2824 members

General Questions

SilverStripe Forums » General Questions » Conditional Fields in the CMS

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1315 Views
  • quamsta
    Avatar
    Community Member
    29 Posts

    Conditional Fields in the CMS Link to this post

    Hi, I have a field in my Page.php getCMSFields function that I want to display on every TOP level page only. I don't want the field to show up in any level higher than 1. Is there a simple variable I can check with an if statement to do this?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Conditional Fields in the CMS Link to this post

    if(!$this->Parent()) {
    $fields->addFieldToTab...
    }

  • quamsta
    Avatar
    Community Member
    29 Posts

    Re: Conditional Fields in the CMS Link to this post

    Hey, thanks for replying. I found Parent() in the API, earlier but I couldn't figure out how to use it.

    I tried your if statement, but it doesn't work. The field doesn't show up anywhere, and when I negate the if-statement you showed me, the field shows up on all pages, regardless of their hierarchy

  • UncleCheese
    Avatar
    4085 Posts

    Re: Conditional Fields in the CMS Link to this post

    It depends on how you have your architecture set up. Find out the parentID and just use that..

    if(!this->Parent())
    $fields->addFieldToTab...
    else
    $fields->addFieldToTab(new LiteralField('foo',$this->ParentID))

    if $this->Parent() is returning true, it will return something other than 0 in the LiteralField, in which case, you're not really at the top level. If you want to leave it the way it is, just do

    if($this->ParentID == $theID)
    //add the field

    or you could even do

    if($this->Parent()->URLSegment == "some-page"))

    you see what I mean..

  • quamsta
    Avatar
    Community Member
    29 Posts

    Re: Conditional Fields in the CMS Link to this post

    I see what you mean. I was able to get my code to work with this statement:

    if(!$this->Parent) {
          $fields->addFieldToTab(

    Thanks for your help!!

  • zim
    Avatar
    Community Member
    135 Posts

    Re: Conditional Fields in the CMS Link to this post

    I want to do this but the other way around. that is I have two image fields that I want to display only on top level... not children.

    (the fields are declared in page.php)

    Specifically on the blog entries in my blog module.

    I tried

    if(!$this->Level(0)) {

    $fields->addFieldToTab("Root.Content.menuImage", new ImageField('menuImage'));
    $fields->addFieldToTab("Root.Content.menuImage", new ImageField('menuImageRoll'));

    }

    but this did not work. fields still appear.

    can anyone help?

    1315 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.