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

Save widget content on every page - nearly solved?


Go to End


3 Posts   2163 Views

Avatar
bones

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",
	);

and

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

and to templatename/templates/Layout/HomePage.ss I added this:

$MyWidgets

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.

Avatar
martimiz

Forum Moderator, 1391 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 %>

Avatar
bones

Community Member, 110 Posts

18 February 2012 at 3:57am

Perfect! Thank you.

:D