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.

Widgets

Discuss SilverStripe Widgets.

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

In Admin Only Show Widget on Certain Pagetypes


Reply

5 Posts   698 Views

Avatar
_Matt

4 April 2014 at 1:11am Community Member, 27 Posts

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.

<?php

class MyWidget extends Widget
{
// Stuff

static $only_available_in = array('MyPageType');

// More stuff etc.

Can anyone help?

Avatar
Willr

4 April 2014 at 5:06pm Forum Moderator, 5511 Posts

$only_available_in looks to be what you want (https://github.com/silverstripe/silverstripe-widgets/blob/master/code/model/Widget.php#L30) I would check to ensure you have marked it as private, got the latest version of widgets module and you've done a flush.

Avatar
_Matt

4 April 2014 at 9:10pm Community Member, 27 Posts

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:

<?php

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?

Avatar
_Matt

5 September 2014 at 10:43pm Community Member, 27 Posts

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.

Avatar
_Matt

6 September 2014 at 1:34am Community Member, 27 Posts

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/Page.ss' file I added a simple conditional block to only output the user defined form widgets if the page type was 'UserDefinedForm':

<% if ClassName = UserDefinedForm %>
$UserDefinedFormWidgetArea
<% 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.