Jump to:

23581 Posts in 19403 Topics by 2893 members

General Questions

SilverStripe Forums » General Questions » Control Widgets

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1354 Views
  • borriej
    Community Member
    267 Posts

    Control Widgets Link to this post


    I have a blog with blog entry's
    some blog entry's contain widgets, one of the widgets is featuredvideo
    ok now i have a page media..

    this media page collects all the media items like video's/pictures/audio from the blog entry's

    So first im doing this:

    class Media_Controller extends Page_Controller {
       function ViewAll($num=9999) {
          $news = DataObject::get_one("BlogHolder");
          return ($news) ? DataObject::get("BlogEntry", "ParentID = $news->ID", "Date DESC", "", $num) : false;

    in the template i do this:

    <% if inSection(video) %>

    <div class="clearfix pbot40">
    <% control ViewAll %>
    <% if Widgets %>
    <div class="MediaVideoController">
    <% end_if %>
    <% end_control %>

    <% end_if %>

    A) I would like to filter on featured video.. so.. if widgets.. if featured video... echo featured video.. now i get alot of empty div's printed in the source code.
    B) When you use featured video you get a div wrapper which contains your video's. So i when i have 3 video's in one article i get them along side eachother.. but (see example) i want all the video's in its own wrapper and nicely floating along side eachother. I would like to bypass the Widget template and just control the videos from the media template. But i dont know how to control widgets..

    Any idea's?

  • Zauberfisch
    Community Member
    30 Posts

    Re: Control Widgets Link to this post

    I never used Widgets before, but by looking into the code it seems Widget is extending DO, so it is rather simple getting all video widgets:

    function getAllVideoWidgets() {
    return DataObject::get('FeaturedvideoWidget');

    <% control getAllVideoWidgets %>
    <div class="Featuredvideo">
    $Content or $WidgetHolder or $forTemplate or something, you need to try around a bit, but I think something like that should work
    <% end_control %>

  • Plato
    Community Member
    26 Posts

    Re: Control Widgets Link to this post

    (3:02:40 AM) borriej [~52aa1a47@littleted.silverstripe.com] entered the room.
    (3:03:07 AM) borriej: hw;;p
    (3:03:11 AM) borriej: hello
    (3:03:25 AM) borriej: im using a function to get all the blog entry
    (3:03:26 AM) borriej: s
    (3:03:42 AM) borriej:    function ViewAll($num=9999) {       $news = DataObject::get_one("BlogHolder");       return ($news) ? DataObject::get("BlogEntry", "ParentID = $news->ID", "Date DESC", "", $num) : false;    }
    (3:04:13 AM) borriej: i want all the blog entry's with featured video displaye
    (3:04:18 AM) borriej: displayed
    (3:04:25 AM) borriej: so i just did $Wdigets
    (3:04:40 AM) borriej: but all blog entry's with any widgets are now being displayed
    (3:05:01 AM) borriej: but i want only the featured video widget
    (3:05:05 AM) borriej: how do i filter this?
    (3:05:10 AM) borriej: tried many things, but did not work
    (3:07:59 AM) borriej: any one?
    (3:08:44 AM) borriej: i need something <% control widgets %><% if Title==Video %>$Widgets <% end_control %><% end_control %>
    (3:08:49 AM) borriej: but this doesent work
    (3:14:23 AM) borriej: Dudes
    (3:14:26 AM) borriej: please help me

    Hi borriej,

    I had typed out a bigger more thorough post, but I lost it somehow and Zauberfisch seems to have covered much of what I said far more succinctly. So I'll just add this:

    I think the biggest problem you're finding is caused by not controlling deep enough in your templates. Anything that comes out through a template should be able to be controlled into. In the above code you put on IRC you controlled into widgets, but then called it again when already inside (resulting in no such variable, thus it didn't work). You wanted to control some specific element of the video widget to get what you wanted.

    Look at your data object (video widget) and see how it's structured. This will help you understand what you can call from in the templates. You'll want to call the $VideoCode (as an example) to get just the code, and you can form your own html containers around it. This will bypass the widget template that's causing your videos to not align nicely 3 wide on the example media page. Other info to help you out can come from inspecting how your widget is included in your page (how to control into it from a page to customise the html there), and how the table in the database is also structured (can help with filtering when you DO::get() )

    In the case that the full HTML (including containing div) is stored in the database (unlikely), you'll have to write a custom getter that fetches the info then strips the containing div before putting the code out. This way you can float everything next to each other.

  • borriej
    Community Member
    267 Posts

    Re: Control Widgets Link to this post

    ok thx for your replies im a bit further now..

    Wel next step. I want a title field to added to the featured video

    so i tried this:


    // Featured Video Widget 0.1 for the SilverStripe Blog Module
    // 10.11.2007
    // By abimanyuraja@gmail.com
    // Save the entire widget folder to the root folder and run "db/build".

    class FeaturedvideoWidget extends Widget {
       static $db = array(
          //start my add
          "VideoName" => "Text",
          // end my add
          "VideoProvider" => "Varchar",
          "Url" => "Text",
          "YouTubeCode" => "Text"
       function getMyParent(){
          return dataObject::get_by_id('BlogEntry', $this->BlogEntryID);
       //public static $youtubeTitle;
       static $title = "Video";
       static $cmsTitle = "Featured Video";
       static $description = "Display one video from Youtube, Google Video or Vimeo. Full sized videos can also be viewed";
       //start my add
       public static $VideoName;
       // end my add   

       public function getTitle() {
          if (!$this->youtubeTitle) {
           $url = "http://gdata.youtube.com/feeds/api/videos/". $this->YouTubeCode;
           $doc = new DOMDocument;
           $this->youtubeTitle = (@$doc->load($url))?$doc->getElementsByTagName("title")->item(0)->nodeValue:'';
          return $this->youtubeTitle;
       function GetFeaturedVideo() {
        try {
          $parsedUrl = parse_url($this->Url);
          //start my add
          $VideoName = $this->VideoName;
          // end my add   
          switch ($this->VideoProvider) {
          case "youtube":
             parse_str($parsedUrl['query'], $id);
             $videoUrl = "http://youtube.com/v/" . $id['v'];
             $videoID = $id['v'];
             $width = 170;
             $height = 80;
          case "googleVideo":
             parse_str($parsedUrl['query'], $id);
             $videoUrl = "http://video.google.com/googleplayer.swf?docid=" . $id['docid'];
             $width = 410;
             $height = 336;
          case "vimeo":
             $id = split("/", $parsedUrl['path']);
             $videoUrl = "http://www.vimeo.com/moogaloop.swf?clip_id=" . $id[1] . "&amp;server=www.vimeo.com&amp;fullscreen=1";
             $width = 480;
             $height = 360;
             $VideoName = "";
             $videoUrl = "";
          $output = new DataObjectSet();

          $output->push(new ArrayData(array(
                   //start my add
                   "VideoName" => $VideoName,
                   // end my add   
                   "Url" => $videoUrl,
                   "Width" => $width,
                   "Height" => $height,
                   "YouTubeCode" => $videoID,
          return $output;
          } catch(Exception $e) {
             return false;

       function getCMSFields() {
          return new FieldSet(
             //start my add
             new TextField("VideoName", "Video Title"),
             // end my add   
             new TextField("Url", "Video URL"),
             new OptionsetField(
                   $name = "VideoProvider",
                   $title = "Video Site",
                   $source = array(
                      "youtube" => "Youtube",
                      "googleVideo" => "Google Video",
                      "vimeo" => "Vimeo"
                   $value = "youtube"
    class FeaturedvideoWidget_Controller extends Widget_Controller {


    But when i fill in the featured video fields (title & link) and hit save.. the fields are empty after the green checkmark..

    When Zauberfisch is in the admin.. he can save it.

    So what is going on?

    I dont understand, did flush and dev/build many times..

    Is my hosting nagging me?

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.