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

Widgets /

Discuss SilverStripe Widgets.

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

In Admin Only Show Widget on Certain Pagetypes

Go to End

5 Posts   4841 Views


Community Member, 29 Posts

4 April 2014 at 1:11am

In Admin I have a widget that I only want to be available on a certain pagetype. I've tried things like the code below, but can't get it to work.


class MyWidget extends Widget
    // Stuff

    static $only_available_in = array('MyPageType');

    // More stuff etc.

Can anyone help?


Forum Moderator, 5523 Posts

4 April 2014 at 5:06pm

$only_available_in looks to be what you want ( I would check to ensure you have marked it as private, got the latest version of widgets module and you've done a flush.


Community Member, 29 Posts

4 April 2014 at 9:10pm

Thanks Willr.

I've tried your suggestions and it's still not working. Basically the widget doesn't appear in the available widgets area when I add that line of code (and I've doubled checked I am on a page with the correct pagetype).

My set up is as follows. I have a pagetype that extends Page, called PageHolder. My widget allows you to list child pages in the widget area. The bare bones of my widget code is as follows:


class PageHolderListPlacementWidget extends Widget
static $cmsTitle = 'Page Holder List Placement Widget';
static $description = 'This widget allows you to position a Page Holder list on the page.';
private static $only_available_in = array('PageHolder');

// The rest of my widget code...

Let me know if you think of anything else?


Community Member, 29 Posts

5 September 2014 at 10:43pm

I've looked into this a bit further and it turns out '$only_available_in' is for widget areas, not for page types. So if you have a sidebar widget area and a footer widget area you can make sure certain widgets are only shown in one or the other.

So, my problem still remains - how can I stop a widget from being shown on a particular page type?

Here's a more specific example:

I am using the userforms module. It inherits all the functionality of my 'Page' pagetype. This is fine, but there is one widget I don't want available on a UserDefinedForm page.

Any help would be gratefully received.


Community Member, 29 Posts

6 September 2014 at 1:34am

Ok, so after a LOT of trial and error and a LOT of thinking about this I've finally come up with a fairly satisfactory solution.

First of all I created a DataExtension for UserDefinedForm.

In this DataExtension I added a new widget area, just for user defined form page types.

This then enabled me to use the $only_available_in property in my widget(s) to specify which widget area (rather than page type) they should be available in. I also removed the default widget area ('$fields->removeByName('Widgets');') so I didn't have them both showing up in the admin interface.

Finally, in my 'templates/[my-theme]/templates/Layout/' file I added a simple conditional block to only output the user defined form widgets if the page type was 'UserDefinedForm':

<% if ClassName = UserDefinedForm %>
<% end_if %>

It's not as simple or elegant as I would like, but it works. It would be much better if widgets had a $allowed_page_types property (so you can just specify an array of page types you want the widget available on), but my knowledge doesn't go far enough to know how to set that up.

Anyway, I hope my example above helps others with a similar problem.