21294 Posts in 5734 Topics by 2602 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1128 Views |
-
Add field to Page only

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?
-
Re: Add field to Page only

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()
Mo
-
Re: Add field to Page only

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>'));
-
Re: Add field to Page only

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") {
-
Re: Add field to Page only

2 September 2012 at 1:16am Last edited: 2 September 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...
-
Re: Add field to Page only

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.
| 1128 Views | ||
|
Page:
1
|
Go to Top |



