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.

Widgets /

Discuss SilverStripe Widgets.

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

My widget breaks silverstripe :(

Go to End

3 Posts   842 Views


Community Member, 4 Posts

19 September 2012 at 4:07am


I've made a widget for displaying some text and/or images.
It seems to work just fine, except one big problem.
It breaks the page .
I don't get any error messages, but it ruins the layout. The page can't be viewed at all in Internet Explorer, the content is all over the place. But it's viewebal in Chrome & Firefox, but the whole page is moved down about 20px..

I've compared my widget to other ones and I can't find what makes silverstripe behave so strange.



class MyWidget extends Widget {
	static $db = array(
		"Title" => "Varchar",
		"Layout" => "Varchar",
		"SecondTitle" => "Varchar",
		"ImageCaption" => "Varchar",
		"Text" => "Text",
		"LinkUrl" => "Varchar",
		"ExtraLinkUrl" => "Varchar"
	static $has_one = array(
		'LinkImage' => 'Image',
		'ExtraLinkImage' => 'Image'
	static $has_many = array();
	static $many_many = array();
	static $belongs_many_many = array();
	static $defaults = array(
		"Title" => "",
		"Layout" => "Bluebox",
		"SecondTitle" => "",
		"ImageCaption" => "",
		"Text" => "",
		"LinkUrl" => "#",
		"ExtraLinkUrl" => "#"

	static $cmsTitle = "My Widget";
	static $description = "En widget för titlar, text, bilder, samt länkar.";
	function getCMSFields() {
		$fields = parent::getCMSFields(); 
		$options = array("Bluebox" => "Bluebox", "ImageLink" => "ImageLink and text", "ImageLink2" => "ImageLink and text with extra ImageLink bellow text.");

			new FieldList(
				new TextField("Title", "Titel"),
			new DropdownField("Layout", "Layout (layouten 'bluebox' visar inga bilder)", $options),
			new TextField("SecondTitle", "Extra title som visas om layout 'Bluebox' är valt."),
			new WidgetImageField("LinkImageID", "Bild"),
			new TextField("ImageCaption", "Fet text som visas direkt under bild"),
			new TextAreaField("Text", "Text"),
			new TextField("LinkUrl", "Länk"),
			new WidgetImageField("ExtraLinkImageID", "Extra Bild som visas under texten."),
			new TextField("ExtraLinkUrl", "Länk om bilden under texten skall länka.")
		$this->extend('updateCMSFields', $fields);
		return $fields;
	function Title() {
		return $this->Title;

class MyWidget_Controller extends Widget_Controller {

	 * @var array
	public static $allowed_actions = array (
	public function init() {
		if (isset($this->LinkImageID) && is_numeric($this->LinkImageID)) {
			$this->LinkImageTag = DataObject::get_by_id('Image',$this->LinkImageID);
		if (isset($this->ExtraLinkImageID) && is_numeric($this->ExtraLinkImageID)) {
			$this->ExtraLinkImageTag = DataObject::get_by_id('Image',$this->ExtraLinkImageID);




<div class="$Layout">
	<% if Layout == "Bluebox" %>
	<% else %>
	<a href="$LinkUrl" title="$Title">
		<% if ImageCaption %>
			<h3 class="center">$ImageCaption</h3>
		<% end_if %>
	<% end_if %>
	<% if Layout == "Bluebox" %>
		<a href="$LinkUrl" title="Läs mer">Läs mer >>></a>
	<% end_if %>
<% if Layout == "ImageLink2" %>
	<a href="$ExtraLinkUrl" class="extra">
<% end_if %>

Thanks, in advance, for any help that could help me find the "bug"



Community Member, 69 Posts

20 September 2012 at 7:01am

Edited: 20/09/2012 7:08am

I think the most relevant is that you edit yous css file and classes if your code is shown right.
Position it in a separate div?
<div id="MyWidget">your content</div>
so in your case in the ss file <div id="Layout"> and not class="$Layout"

css file for instance in yourtheme layout.css:

#MyWidget {
width: 200px;
float: left;
margin: 10px 10px 10px 10px;
text-align: left;

Mostly bad css programming breaks up the site


Community Member, 4 Posts

20 September 2012 at 7:10am

Thanks.. but that wasn't it.

I just found what was causing the problems.

I had put


at the end of my files.

Removing that solved everything :D