I added a custom tab to the Page page type. I've got it working using an include in Page.ss. How can I tell it only to show up if there is text in the text field on that tab and if there's not I don't want it to be included at all?
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
Perhaps you could try something like:
<% if SomeField %>
<% include SomeFile %>
<% end_if %>
That did it! Well I had to tweak it a bit but that was what I needed.
Here's what I did so it's documented in case someone else needs to know:
1. I added an "Announcement" tab to Page.php.
class Page extends SiteTree {
static $db = array(
"AnnouncementContent" => "Text",
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Announcement', new TextField('AnnouncementContent'));
return $fields;
}
static $has_one = array(
);
}
2. I created Announcement.ss and put in in the includes folder of my theme.
<% control Page(home) %>
<% if AnnouncementContent %>
<div id="announcement">
$AnnouncementContent
</div>
<% end_if %>
<% end_control %>
3. I added the include to the appropriate place in Page.ss
Now What would make it better is to be able to remove the tab from all the pages other than Home. But do that without creating a HomePage page type. Home is just a Page page type like all the rest. With the page control in Announcement.ss it will only take the text that's entered in the Announcement tab on the home page. But it would make it less confusing for the user if that tab was only there on the page that it pulls from.
sonicparke-
You're really close! You could try adding an if statement around your addFieldToTab method:
if ('Home' == $this->Title) {
$fields->addFieldToTab( 'Root.Content.Announcement', new TextField( 'AnnouncementContent' ) );
return $fields;
}
Depending on what you need, you could also compare that to MenuTitle instead of Title.
Hope that helps!