2011-02-07 07:48:44 +01:00
# Widgets
## Introduction
2011-03-08 22:05:51 +01:00
[Widgets ](http://silverstripe.org/widgets ) are small pieces of functionality such as showing the latest Comments or Flickr Photos. They normally display on
the sidebar of your website. To check out a what a [Widget ](http://silverstripe.org/widgets ) can do watch the
[Widget video ](http://silverstripe.com/assets/screencasts/SilverStripe-Blog-DragDrop-Widgets.swf ) and try out the
[demo site ](http://demo.silverstripe.org/ )
2011-02-07 07:48:44 +01:00
2011-03-08 22:05:51 +01:00
## How to Use A Widget
2011-02-07 07:48:44 +01:00
### Downloading and Contributing Widgets
* To download widgets visit [Widgets section ](http://silverstripe.org/widgets )
* Upload widgets you want to share to
[http://silverstripe.org/widgets/manage/add ](http://silverstripe.org/widgets/manage/add ). Make sure you read the
packaging instructions at the bottom of the page about how to make your widget package.
2011-03-08 22:05:51 +01:00
2011-02-07 07:48:44 +01:00
### Installing a widget
By following the "Packaging" rules below, widgets are easily installed.
2011-03-08 22:05:51 +01:00
* Install the [blog module ](http://www.silverstripe.org/blog-module/ ) (by default only the Blog has widgets enabled)
* Download the file and unzip to the main folder of your SilverStripe website, e.g. to `/widget_<widget-name>/` . The folder
2011-02-07 07:48:44 +01:00
will contain a few files, which generally won't need editing or reading.
2011-03-08 22:05:51 +01:00
* Run `http://my-website.com/dev/build`
* Login to the CMS and go to the 'Blog' page. Choose the "widgets" tab and drag n drop the new widget to activate it.
* Your blog will now have the widget shown
2011-02-07 07:48:44 +01:00
### Adding widgets to other pages
2011-03-08 22:05:51 +01:00
< div class = "notice" markdown = '1' >
As of version 2.2.1 there is a way to add widgets to other pages. In future releases we will hopefully make widgets part
of SiteTree therefore available on every page.
< / div >
You have to do a couple things to get a Widget to work on a page.
2011-02-07 07:48:44 +01:00
First step is to add an WidgetArea to the Database to store the widget details. Then you have to edit the CMS to add a
Widget Form to manage the widgets. An example of this is below
2011-03-08 22:05:51 +01:00
**mysite/code/Page.php**
2011-02-07 07:48:44 +01:00
:::php
class Page extends SiteTree {
...
static $has_one = array(
"Sidebar" => "WidgetArea",
);
2012-01-30 23:13:42 +01:00
public function getCMSFields() {
2011-02-07 07:48:44 +01:00
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.Widgets", new WidgetAreaEditor("Sidebar"));
return $fields;
}
....
}
2011-03-08 22:05:51 +01:00
Then in your Template you need to call $SideBar wherever you want to render the widget
2011-02-07 07:48:44 +01:00
2011-03-08 22:05:51 +01:00
For example: using the blackcandy theme I put this piece of code above the closing `</div>`
2011-02-07 07:48:44 +01:00
2011-03-08 22:05:51 +01:00
**themes/blackcandy/templates/Includes/Sidebar.ss**
2011-02-07 07:48:44 +01:00
:::ss
$Sidebar
## Writing your own widgets
To create a Widget you need at least three files - a php file containing the class, a template file of the same name and
a config file called *_config.php* (if you dont need any config options for the widget to work then you can make it
blank). Each widget should be in its own folder like widgets_widgetName/
After installing or creating a new widget, **make sure to run db/build?flush=1** at the end of the URL, *before*
attempting to use it.
The class should extend the Widget class, and must specify three static variables - $title, the title that will appear
in the rendered widget (eg Photos), $cmsTitle, a more descriptive title that will appear in the cms editor (eg Flickr
Photos), and $description, a short description that will appear in the cms editor (eg This widget shows photos from
Flickr). The class may also specify functions to be used in the template like a page type can.
If a Widget has configurable options, then it can specify a number of database fields to store these options in via the
2011-10-28 03:37:27 +02:00
static $db array, and also specify a getCMSFields function that returns a !FieldList, much the same way as a page type
2011-02-07 07:48:44 +01:00
does.
An example widget is below:
**FlickrWidget.php**
:::php
< ?php
class FlickrWidget extends Widget {
static $db = array(
"User" => "Varchar",
"Photoset" => "Varchar",
"Tags" => "Varchar",
"NumberToShow" => "Int"
);
static $defaults = array(
"NumberToShow" => 8
);
static $title = "Photos";
static $cmsTitle = "Flickr Photos";
static $description = "Shows flickr photos.";
2012-01-30 23:13:42 +01:00
public function Photos() {
2011-02-07 07:48:44 +01:00
Requirements::javascript("sapphire/thirdparty/prototype/prototype.js");
Requirements::javascript("sapphire/thirdparty/scriptaculous/effects.js");
Requirements::javascript("mashups/javascript/lightbox.js");
Requirements::css("mashups/css/lightbox.css");
$flickr = new FlickrService();
if($this->Photoset == "") {
$photos = $flickr->getPhotos($this->Tags, $this->User, $this->NumberToShow, 1);
} else {
$photos = $flickr->getPhotoSet($this->Photoset, $this->User, $this->NumberToShow, 1);
}
$output = new DataObjectSet();
foreach($photos->PhotoItems as $photo) {
$output->push(new ArrayData(array(
"Title" => $photo->title,
"Link" => "http://farm1.static.flickr.com/" . $photo->image_path .".jpg",
"Image" => "http://farm1.static.flickr.com/" .$photo->image_path. "_s.jpg"
)));
}
return $output;
}
2012-01-30 23:13:42 +01:00
public function getCMSFields() {
2011-10-28 03:37:27 +02:00
return new FieldList(
2011-02-07 07:48:44 +01:00
new TextField("User", "User"),
new TextField("PhotoSet", "Photo Set"),
new TextField("Tags", "Tags"),
new NumericField("NumberToShow", "Number to Show")
);
}
}
?>
**FlickrWidget.ss**
:::php
< % control Photos %>
< a href = "$Link" rel = "lightbox" title = "$Title" > < img src = "$Image" alt = "$Title" / > < / a >
< % end_control %>
## Extending and Customizing
### Rendering a $Widget Individually
To call a single Widget in a page - without adding a widget area in the CMS for you to add / delete the widgets, you can
define a merge variable in the Page Controller and include it in the Page Template.
This example creates an RSSWidget with the SilverStripe blog feed.
:::php
< ?php
2012-01-30 23:13:42 +01:00
public function SilverStripeFeed() {
2011-02-07 07:48:44 +01:00
$widget = new RSSWidget();
$widget->RssUrl = "http://feeds.feedburner.com/silverstripe-blog";
return $widget->renderWith("WidgetHolder");
}
?>
To render the widget, simply include $SilverStripeFeed in your template:
:::ss
$SilverStripeFeed
As directed in the definition of SilverStripeFeed(), the Widget will be rendered through the WidgetHolder template. This
2011-03-08 22:05:51 +01:00
is pre-defined at `/sapphire/templates/WidgetHolder.ss` and simply consists of:
2011-02-07 07:48:44 +01:00
:::ss
< div class = "WidgetHolder" >
< h3 > $Title< / h3 >
$Content
< / div >
You can override the WidgetHolder.ss and Widget.ss templates in your theme too by adding WidgetHolder and Widget
2011-03-08 22:05:51 +01:00
templates to `themes/myThemeName/templates/Includes/`
2011-02-07 07:48:44 +01:00
### Changing the title of your widget
To change the title of your widget, you need to override the Title() method. By default, this simply returns the $title
variable. For example, to set your widgets title to 'Hello World!', you could use:
2011-03-08 22:05:51 +01:00
**widgets_yourWidget/YourWidgetWidget.php**
2011-02-07 07:48:44 +01:00
:::php
2012-01-30 23:13:42 +01:00
public function Title() {
2011-02-07 07:48:44 +01:00
return "Hello World!";
}
but, you can do exactly the same by setting your $title variable.
A more common reason for overriding Title() is to allow the title to be set in the CMS. Say you had a text field in your
widget called WidgetTitle, that you wish to use as your title. If nothing is set, then you'll use your default title.
This is similar to the RSS Widget in the blog module.
:::php
2012-01-30 23:13:42 +01:00
public function Title() {
2011-02-07 07:48:44 +01:00
return $this->WidgetTitle ? $this->WidgetTitle : self::$title;
}
This returns the value inputted in the CMS, if it's set or what is in the $title variable if it isn't.
### Forms within Widgets
To implement a form inside a widget, you need to implement a custom controller for your widget to return this form. Make
sure that your controller follows the usual naming conventions, and it will be automatically picked up by the
`[api:WidgetArea]` rendering in your *Page.ss* template.
2011-03-08 22:05:51 +01:00
**mysite/code/MyWidget.php**
2011-02-07 07:48:44 +01:00
:::php
class MyWidget extends Widget {
static $db = array(
'TestValue' => 'Text'
);
}
class MyWidget_Controller extends Widget_Controller {
2012-01-30 23:13:42 +01:00
public function MyFormName() {
2011-02-07 07:48:44 +01:00
return new Form(
$this,
'MyFormName',
2011-10-28 03:37:27 +02:00
new FieldList(
2011-02-07 07:48:44 +01:00
new TextField('TestValue')
),
2011-10-28 03:37:27 +02:00
new FieldList(
2011-02-07 07:48:44 +01:00
new FormAction('doAction')
)
);
}
2012-01-30 23:13:42 +01:00
public function doAction($data, $form) {
2011-02-07 07:48:44 +01:00
// $this->widget points to the widget
}
}
To output this form, modify your widget template.
2011-03-08 22:05:51 +01:00
**mysite/templates/MyWidget.ss**
2011-02-07 07:48:44 +01:00
:::ss
$Content
$MyFormName
2011-03-08 22:05:51 +01:00
< div class = "notice" markdown = '1' >
**Note:** The necessary controller actions are only present in subclasses of `[api:Page_Controller]` . To use
2011-02-07 07:48:44 +01:00
widget forms in other controller subclasses, have a look at *ContentController->handleWidget()* and
*ContentController::$url_handlers*.
2011-03-08 22:05:51 +01:00
< / div >
2011-02-07 07:48:44 +01:00
See an [alternative recipe for SilverStripe 2.3 or earlier ](http://doc.silverstripe.org/old/recipes/widget-forms-2.3 ).
2011-03-08 22:05:51 +01:00
## But what if I have widgets on my blog currently??
2011-02-07 07:48:44 +01:00
If you currently have a blog installed, the widget fields are going to double up on those pages (as the blog extends the
Page class). One way to fix this is to comment out line 30 in BlogHolder.php and remove the DB entry by running a
2011-03-08 22:05:51 +01:00
`http://www.mysite.com/db/build` .
2011-02-07 07:48:44 +01:00
2011-03-08 22:05:51 +01:00
**blog/code/BlogHolder.php**
2011-02-07 07:48:44 +01:00
:::php
< ?php
class BlogHolder extends Page {
........
static $has_one = array(
// "SideBar" => "WidgetArea", COMMENT OUT
'Newsletter' => 'NewsletterType'
.......
2012-01-30 23:13:42 +01:00
public function getCMSFields() {
2011-02-07 07:48:44 +01:00
$fields = parent::getCMSFields();
2011-04-15 06:36:22 +02:00
$fields->removeFieldFromTab("Root.Content","Content");
2011-02-07 07:48:44 +01:00
// $fields->addFieldToTab("Root.Content.Widgets", new WidgetAreaEditor("SideBar")); COMMENT OUT
........
Then you can use the Widget area you defined on Page.php
## Releasing Your Widget
### Packaging
For a widget to be put in our official widget database they must follow this convention - If the name of your widget was
2011-03-08 22:05:51 +01:00
"YourName" then:
2011-02-07 07:48:44 +01:00
#### File Structure for your widget
You should have a folder called widget_YourName in the top level (the one with sapphire, cms..) with all your files. See
the example below. Your widget **MUST** have at least 1 Template file, 1 PHP file, the README File
[(Example) ](http://open.silverstripe.com/browser/modules/widgets/twitter/trunk/README )and an _config.php file for
configuration. If you dont need any config options for the widget to work then you still need an _config.php by you can
make it blank
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.
2011-03-08 22:05:51 +01:00
*widget_name/_config.php*
2011-02-07 07:48:44 +01:00
:::php
<?php /* */ ?>
2011-03-08 22:05:51 +01:00
**Example Widget Structure**
2011-02-07 07:48:44 +01:00
![](_images/widget_demo.gif)
2011-03-08 22:05:51 +01:00
2011-02-07 07:48:44 +01:00
#### How to make the Package
2011-03-08 22:05:51 +01:00
* Make a tar.gz file called widgets_YourName-0.1.tar.gz (where 0.1 is the version number).
* Ensure when you "unzip" the compressed file it has everything the "widgets_YourName" folder with everything inside
2011-02-07 07:48:44 +01:00
it.
* If made official, it will be given these locations at silverstripe.com:
* SVN location: http://svn.silverstripe.com/open/modules/widgets/flickr/trunk
* Official download: http://www.silverstripe.com/assets/downloads/widgets/widgets_flickr-0.1.1.tar.gz