Jump to:

23452 Posts in 18901 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Add field to Page only

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: 1481 Views
  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Add field to Page only Link to this post

    I want to add a custom field to Page.php like this:

    $fields->addFieldToTab("Root.Content.Main", new TextField('Foobar','Foobar'));

    The problem is that the field ALSO appears in all other page types.

    I can manually remove it from each page type but is there a way JUST to add it to a Page?

  • Mo
    Avatar
    Community Member
    508 Posts

    Re: Add field to Page only Link to this post

    If you mean you only want the field to appear on Page, and not its descendants, you might be able to do something like:

    if($this->ClassName() == "Page")
    $fields->addFieldToTab("Root.Content.Main", new TextField('Foobar','Foobar'));

    Not sure if that will work, or if you can call $this->ClassName() from within getCmsFields()

    Mo

  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Re: Add field to Page only Link to this post

    Thanks. Your idea worked however I had to slightly modify your code. Here is a working example:

    if($this->ClassName == "Page") $fields->addFieldToTab("Root.Content.Special", new LiteralField('CustomCode','<h2>Hello World</h2>'));

  • Stef87
    Avatar
    Community Member
    66 Posts

    Re: Add field to Page only Link to this post

    Hi I know this is an old post but I'm getting desperate. I'm trying to do exactly this but with a page that extends Page.php. However it doesn't work. It's like the criteria of the if statement is never met.

    if ($this->ClassName == "Movies") {

  • martimiz
    Avatar
    Forum Moderator
    1078 Posts

    Re: Add field to Page only Link to this post

    Hi Stef87

    If Movies is the classname of your Page, and if you placed this code in your pages getCMSFields() function, this should work.

    But this is only necessary if there are other classes that extend 'Movies', for which you don't want the field(s) to be created. If not, you can just subclass getCMSFields() in your Movies object, like this:

    function getCMSFields() {
       $fields = parent::getCMSFields();
       // add/remove whatever you like...
       return $fields;
    }

    On the otherhand, if you want the field to be present on a certain page only, and not on its children, then check on the ID, or on the Title instead of the classname. Or create a new PageType just for that page.

    If this doesn't help you, then post some code, so we can see what's going on...

    [EDIT] Please don't crosspost your questions - I found 2 other variations of this post here and here. It won't get you a response any quicker and it makes it harder for others that have the same issue, to find the solution...

  • Stef87
    Avatar
    Community Member
    66 Posts

    Re: Add field to Page only Link to this post

    Thank you for your reply martimiz. I apologise, I have deleted the other two questions.

    In terms of my problem I think your answer has solved it for me (I have to test it though). Checking the title may be what I have to do. I will let you know how I get on.

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