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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Widgets /

Discuss SilverStripe Widgets.

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

A single widget area instance shared across whole site (not 1 widget area instance per page)...


Go to End


1142 Views

Avatar
vwd

Community Member, 166 Posts

7 November 2011 at 6:29pm

Hi,

The site we're building at the moment will have an identical widget area instance across all pages of the site. Previously, we had the widget area added to the Page class, but obviously this meant that each Page object/child will have its own instance of a widget area.

So if we were to have the same widgets across all the pages, we would have to manually add them to each page via the Widget editor tab.

To have a site-wide widget area instance, we've decorated SiteConfig with the widget area. Now we get a widget area under SiteConfig, and whatever widgets we add here should be shared across the site.

CustomSiteConfig.php

    class CustomSiteConfig extends DataObjectDecorator {
        function extraStatics() {
            return array(
                'has_one' => array(
                    'SiteWideWidgetArea' => 'WidgetArea'
                )
            );
        }

        public function updateCMSFields(FieldSet $fields) {
            $fields->addFieldToTab("Root.Widgets", new WidgetAreaEditor("SiteWideWidgetArea"));
        }
    }

Template: eg Page.ss

    <snip>
    $SiteConfig.SiteWideWidgetArea
    <snip>

Is there anything wrong with this approach?

And also, in the past, some have commented that they've had difficulty to have 1 Widget area per page... But it hasn't been a problem getting more than 1 widget area... Are we doing the right thing here? Seems legit...

Page.php

class Page extends SiteTree {

	public static $has_one = array(
        "BottomWidgetArea" => "WidgetArea",
        "Bottom2WidgetArea" => "WidgetArea"
	);

    public function getCMSFields() {
        $fields = parent::getCMSFields();

        $fields->addFieldToTab("Root.Content.Widgets", new WidgetAreaEditor("PageWidgetArea"));
        $fields->addFieldToTab("Root.Content.Widgets2", new WidgetAreaEditor("AnotherPageWidgetArea"));

        return $fields;
    }
}

Template: page.ss

    <snip>
    $PageWidgetArea
    $AnotherPageWidgetArea
    <snip>

Thanks for your input...

VWD.