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.

Widgets /

Discuss SilverStripe Widgets.

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

Is there a better way to add the same widgets to all pages?


Go to End


11 Posts   4490 Views

Avatar
MikeOne

Community Member, 40 Posts

15 October 2009 at 2:31am

Hi,

I have been struggeling quite a lot (I'm a newbie) to add a sidebar to all pages of the site that contains the same widgets.
The requirements are:

1) Have one place in the CMS to add widgets
2) Show those widgets on all pages of pagetype 'Page' or anything that inherits from it.

Now - I have got it working, but I was wondering if there is a better way of doing it. This is what I did:

Created a 'SidebarPage' page type that allows the editors to add the widgets:

--------------------------------------------

class SidebarPage extends Page{

static $has_one = array(
"Sidebar" => "WidgetArea",
);

static $defaults = array(
'ShowInMenus' => 0,
'ShowInSearch' => 0
);

function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.Widgets", new WidgetAreaEditor("Sidebar"));
return $fields;
}

}

class SidebarPage_Controller extends Page_Controller{

}
--------------------------------------------

Added a SidebarPage.ss:

--------------------------------------------

$Sidebar

--------------------------------------------

And then in mysite/code/page.php - page_controller I added:

--------------------------------------------

function getSidebar(){
$sidebarPage = DataObject::get_one('SidebarPage');
return ($sidebarPage) ? $sidebarPage->renderWith("SidebarPage") : false;
}

--------------------------------------------

Now in Page.ss (or any other template), I can render the sidebarwidgets by simply adding $getSideBar in the content.

Especially the 'renderWith' part took ages for me to find out. Is there a simpler or better way to do this? Or am I spot on?

Thanks,
Mike.

Avatar
MikeOne

Community Member, 40 Posts

17 October 2009 at 12:10am

Nobody has any feedback on this? I really like to know if this is implemented 'correctly'. I know it's working but what I don't know if there is an easier way - or if I didn't break MVC principles etc...

Thanks!

Mike.

Avatar
dhensby

Community Member, 253 Posts

17 October 2009 at 12:30am

Hi MikeOne,

I would say it's a bit of an odd way to do it, certainly not how i would.

This is how i did my WidgetArea so that pages that didn't have widgets defined:

function myWidgets() {
		if(!$this->Widgets()->Widgets()->Count()) { // if no widgets are assigned for the page, then we get the parent's widget
			if ($this->Parent()->exists() && $this->Parent()->Widgets()->Widgets()->Count()) //if the parent has widgets, we get them
			{
				return $this->Parent()->Widgets();
			}
			else { // if the parent doesnt have widgets, just pick some random ones :)
				return DataObject::get_one('WidgetArea', /* Make sure that we pick a widget area that actually has widgets */'`WidgetArea`.ID IN (SELECT DISTINCT `ParentID` FROM `Widget`)','RAND()');
			}
		}
		else { //there are widgets for this page
			return $this->Widgets();
		}
	}

So instead of calling $Sidebar in my template, i call $myWidgets.

I'm not sure why you are using render with, etc. Surely all you need to to is grab the relevant WidgetArea and then output it in the place specified by the template...

Avatar
MikeOne

Community Member, 40 Posts

17 October 2009 at 1:34am

Ah cool - thanks for the reply.

The way you do it seems cleaner, however - where do I add my widget tab in the CMS in your case? The site has quite a flat structure so i need to be able to show widgets NOT from a parent - but from a sibbling. Does that make sense?

So suppose I have 3 pages:

- home
- contact us
- about us

I need one place where I can add widgets - and they will have to show on all of these 3 pages. Will your solution cater for that?

Thanks again for your help.

Mike.

Avatar
MikeOne

Community Member, 40 Posts

17 October 2009 at 1:57am

I wonder though if a combination of you code and my code is possible.. the reason why I had to use renderWith (I guess??) is because without it - it would just return a reference to the sidebar - but without a view attached to it.

maybe i could do the following:

instead of:

function getSidebar(){
$sidebarPage = DataObject::get_one('SidebarPage');
return ($sidebarPage) ? $sidebarPage->renderWith("SidebarPage") : false;
}

I could do:

function getSidebar(){
$sidebarPage = DataObject::get_one('SidebarPage');
return ($sidebarPage) ? $sidebarPage->Widgets() : false;
}

Does that make sense? i will try this this evening and see if it works..

Avatar
dhensby

Community Member, 253 Posts

17 October 2009 at 2:57am

From your earlier reply i see that you are trying to define the widgets on one page and then use those widgets everywhere, where as I have widgets for all the pages and then if the widgets arent defined for a particular page, i go and get them.

Yer, give combining them a go.

But for your return condition you need:

return ($sidebarPage->exists()) ? $sidebarPage->Widgets() : false;

Avatar
MikeOne

Community Member, 40 Posts

17 October 2009 at 3:09am

Thanks for the advice - I will give this a go!

Avatar
yurigoul

Community Member, 203 Posts

28 October 2009 at 8:43am

From the looks of it, this looks like a great way to get one place to add and edit widgets for all the pages in your site.

The only thing I am wondering about is if it would be possible to get rid of the other tabs (Main - Metadata)

Without those tabs it would be much clearer what the function of the page is.

Go to Top