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

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


Go to End


1590 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.