Hiya,
Not sure this is the 'right' way but it certainly works with a lot less code!
If the page you are viewing has widgets, it will use them. If it doesn't have any, it will try use the parent's widgets. If the parent has none it will use the widgets of the Homepage (URL segment = 'home').
This presumes you call $Sidebar in the template and that you have set the $has_one like so:
$has_one = array('WidgetArea' => 'WidgetArea');
function Sidebar()
{
if ($this->WidgetArea()->Widgets()->Count())
{
return $this->WidgetArea();
}
else if ($this->parent()->exists() && $this->parent()->WidgetArea()->Widgets()->Count())
{
return $this->parent()->WidgetArea();
}
else
{
$home = DataObject::get_one("Page", "URLSegment = 'home'");
return $home->WidgetArea();
}
}
I apologise in advance if this has syntax errors - I am not near a SS install to test.
Cheers,
Josh
ps: my indentations are not showing. Grr...