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.

Archive /

Our old forums are still available as a read-only archive.

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

Adding widget options in _config.php


Go to End


6 Posts   1829 Views

Avatar
spenniec

Community Member, 37 Posts

24 April 2008 at 8:09am

Edited: 24/04/2008 8:10am

When developing a widget, how do I add global options/variables in the _config.php file and then reference them in the widget class?

eg:

in _config.php specify something like
$myURL = 'http:.//www.example.com';

in class MyWidget extends Widget {
use $myURL somewhere in code

Avatar
Blynx

Community Member, 20 Posts

24 April 2008 at 8:26am

In think the way it's intended to be is not to set the options in the _config.php, but create the widget in way where you can set these fields in the CMS, when you add the widget.

Have a look at the code of the RSS-widget, it's a very simple example, and easy to see how that works.

Avatar
Willr

Forum Moderator, 5523 Posts

24 April 2008 at 6:26pm

Edited: 24/04/2008 6:26pm

You can set a static varible in the widget class like static $MyVar = "Default Value" then in your _config you can set WidgetName::$MyVar = "My New Value" checkout a couple of the widgets to see how they do it for reference.

Avatar
spenniec

Community Member, 37 Posts

25 April 2008 at 1:19am

Will,
Thanks for that, however I was thinking about doing it the other way around.
My widget would be making some API calls via an external URL. This URL would be the same for every instance of the widget so wouldn't need to be set in the CMS, just when the widget is first setup.
I was thinking about putting the URL in the _config file so that setting up the widget involved changing the value in there, not in the class file. NB there will actually be a number of variables which need to be set.
I tried setting the variables as global in the _config but this didn't work.
I guess I'll declare them static at the top of the class file to be configured by the person who installs the widget.

I just wondered if it was possible the way I was thinking.

Does this mean the _config file is loaded after the widget class is created?

Cheers

Avatar
Sam

Administrator, 690 Posts

25 April 2008 at 9:49am

Edited: 25/04/2008 9:52am

The standard way we solve this problem is to make a static method on MyWidget, like MyWidget::set_url().

You then call that method from _config.php

In terms of load ordering, MyWidget.php is loaded the first time MyWidget is referenced, so putting MyWidget::$url = "bla"; into you _config.php will work too.

There's been discussion of building a more generic configuration API for all these settings, rather than having lots of static methods and variables, but it's early days yet.

By the way, the decision over whether to configure a widget in _config.php or in the CMS is important:

* If the setting is the kind of thing that a website author, familiar with common business apps such as Word and Outlook, would understand - then make it configurable in the CMS.
* If the setting is the kind of thing that the person setting up the website - doing the design and/or development - would understand, then make it configurable in the _config.php file.

This way, the CMS remains an application designed for content authors, and not developers.

Avatar
spenniec

Community Member, 37 Posts

25 April 2008 at 4:14pm

Re-reading Will's post I realised that I didn't read his advice properly. Tut, tut.
Declare it in the class and set it in the _config file.
Thanks Guys, sorted!