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

Adding widgets programatically


Go to End


2 Posts   2276 Views

Avatar
Mo

Community Member, 541 Posts

9 December 2009 at 12:55am

Hi All,

I was just wondering if there was a way of adding widgets to the widget area via code?

Can I add something in my _config.php file? Or extend the relevent class?

I am mainly looking at the blog module for doing this, and I cant hack the module, because it is pulled in via SVN externals.

Any thoughts?

Mo

Avatar
Willr

Forum Moderator, 5523 Posts

9 December 2009 at 9:31am

The only way to add them programmatically is to set the ParentID to the ID of the widget the that of the widget area. Something like..

$widget = new Widget();
$widget->ParentID = $widgetarea->ID;
$widget->write();

Rather then put that in your _config you could probably put that in your requireDefaultRecords() function on page so rebuilding the database triggers the check. You would populate $widgetarea by doing a query on the blogholder.