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.

General Questions /

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

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

Add field to Page only

Go to End

6 Posts   2529 Views


Community Member, 197 Posts

5 August 2010 at 7:57am

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?


Community Member, 541 Posts

5 August 2010 at 10:27am

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()



Community Member, 197 Posts

5 August 2010 at 10:53am

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>'));


Community Member, 66 Posts

1 September 2012 at 3:48am

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") { 


Forum Moderator, 1391 Posts

2 September 2012 at 1:16am

Edited: 02/09/2012 1:34am

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


Community Member, 66 Posts

2 September 2012 at 2:41am

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.