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, simon_w

My widget breaks silverstripe :(


Reply

3 Posts   703 Views

Avatar
oscarw_89

19 September 2012 at 4:07am Community Member, 4 Posts

Hi

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.

mysite/code/MyWidget.php

<?php

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.");
      
      $fields->merge(

         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() {
      parent::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);
      }
      
   }

}

?>

mysite/templates/MyWidget.ss

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

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

//Oscar

Avatar
haantje72

20 September 2012 at 7:01am (Last edited: 20 September 2012 7:08am), Community Member, 69 Posts

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

Avatar
oscarw_89

20 September 2012 at 7:10am Community Member, 4 Posts

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