762 Posts in 312 Topics by 291 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 446 Views |
-
Save widget content on every page - nearly solved?

17 February 2012 at 7:31am Last edited: 17 February 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.
-
Re: Save widget content on every page - nearly solved?

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 %>
-
Re: Save widget content on every page - nearly solved?

18 February 2012 at 3:57am
Perfect! Thank you.
| 446 Views | ||
|
Page:
1
|
Go to Top |

