Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Adding widget options in _config.php

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

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

Page: 1
Go to End
Author Topic: 1500 Views
  • spenniec
    Avatar
    Community Member
    37 Posts

    Adding widget options in _config.php Link to this post

    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

  • Blynx
    Avatar
    Community Member
    20 Posts

    Re: Adding widget options in _config.php Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Adding widget options in _config.php Link to this post

    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.

  • spenniec
    Avatar
    Community Member
    37 Posts

    Re: Adding widget options in _config.php Link to this post

    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

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: Adding widget options in _config.php Link to this post

    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.

  • spenniec
    Avatar
    Community Member
    37 Posts

    Re: Adding widget options in _config.php Link to this post

    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!

    1500 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.