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.

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.

Archive /

Our old forums are still available as a read-only archive.

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

if page control


Go to End


4 Posts   2230 Views

Avatar
sonicparke

74 Posts

11 July 2008 at 5:27pm

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?

Avatar
Grayzag (aka ajshort)

29 Posts

11 July 2008 at 7:24pm

Edited: 11/07/2008 7:25pm

Perhaps you could try something like:

<% if SomeField %>
    <% include SomeFile %>
<% end_if %>

Avatar
sonicparke

74 Posts

11 July 2008 at 8:33pm

Edited: 11/07/2008 8:38pm

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.

Avatar
BLU42 Media

Community Member, 71 Posts

13 July 2008 at 3:37pm

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!