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, Ed, biapar, Willr, Ingo, swaiba

Save widget content on every page - nearly solved?

Go to End

3 Posts   988 Views


Community Member, 110 Posts

17 February 2012 at 7:31am

Edited: 17/02/2012 7:32am

I want to add the same widgets to every page on my website, so adding/editing widgets on my HomePage updates every page.

I've followed the tutorial, and am SO close to getting this to work. I just need a little help on the very last stage.

In mysite/code/HomePage.php, I've added:

	static $has_one = array(
		"MyWidgets" => "WidgetArea",


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

and to templatename/templates/Layout/ I added this:


So far so good. This works great.

However, now I need to get this data onto all other pages. $MyWidgets doesn't work on other pages without a little help, and that's where I'm getting stuck.
I've tried the following on mysite/code/Page.php, but it doesn't work.

class Page_Controller extends ContentController {
	public function getMyWidgets(){ 
		$obj = DataObject::get_one('HomePage'); 
	return $obj->MyWidgets;

I would be very grateful if someone could help me with this last part, please. Thank you.


Forum Moderator, 1203 Posts

18 February 2012 at 12:51am

A really simple way to do this, that seems to work - but I haven't tested it any further, so that'll be up to you :-)

Suppose you want to reproduce your homepage widgets on every other page. Then in your Page_Controller:

	function getWidgetPage() {
		return Dataobject::get('SiteTree', "URLSegment = 'home'");

And in your template:

<% control getWidgetPage %>$MyWidgets<% end_control %>


Community Member, 110 Posts

18 February 2012 at 3:57am

Perfect! Thank you.