Jump to:

17452 Posts in 4473 Topics by 1971 members


SilverStripe Forums » Archive » New Module for pages with embedded YouTube video

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: 1661 Views
  • orientel
    Community Member
    2 Posts

    New Module for pages with embedded YouTube video Link to this post

    I'm trying to learn SilverStripe and I created a simple module to include an embedded youtube video in a page from CMS.

    It works quite correctly, the only "bug" is that if a parameter (Border) has zero value it is not considered. Did I use a wrong data type? I tried varchar, text, int and if border is not zero (e.g. "Border=1") it works, if Border is equal to 0 not (it becomes "Border="):

    class YouTubeVideo extends Page {

    static $db = array(
       "Video" => "Varchar",
       "Color1" => "Varchar",
       "Color2" => "Varchar",
       "Border" => "Varchar",
       "Wmode" => "Varchar",
       "Width" => "Varchar",
       "Height" => "Varchar"

    function getCMSFields($cms) {
    $fields = parent::getCMSFields($cms);
    $fields->addFieldToTab("Root.Content.Main", new TextField("Video","YouTube Video ID"));
    $fields->addFieldToTab("Root.Content.Main", new TextField("Color1","Color 1", "0xd6d6d6"));
    $fields->addFieldToTab("Root.Content.Main", new TextField("Color2","Color 2", "0xf0f0f0"));
    $fields->addFieldToTab("Root.Content.Main", new TextField("Border","Border", "0"));
    $fields->addFieldToTab("Root.Content.Main", new TextField("Wmode","Wmode", "transparent"));
    $fields->addFieldToTab("Root.Content.Main", new TextField("Width","Width", "425"));
    $fields->addFieldToTab("Root.Content.Main", new TextField("Height","Height", "355"));
    return $fields;


    class YouTubeVideo_Controller extends Page_Controller {

    function Content(){
             return $this->Content.$this->EmbeddedVideo();

    function EmbeddedVideo(){
          global $YouTubeUrl;

       $video = '<object width="'.$this->Width.'" height="'.$this->Height.'"><param name="movie" value="'.$YouTubeUrl.$this->Video.'&rel=1&color1='.$this->Color1.'&color2='.$this->Color1.'&border='.$this->Border.'"></param><param name="wmode" value="'.$this->Wmode.'"></param><embed src="'.$YouTubeUrl.$this->Video.'&rel=1&color1='.$this->Color1.'&color2='.$this->Color2.'border='.$this->Border.'" type="application/x-shockwave-flash" wmode="'.$this->Wmode.'" width="'.$this->Width.'" height="'.$this->Height.'"></embed></object>';

          return $video;


    Download module (save target as...)

  • Sean
    Forum Moderator
    921 Posts

    Re: New Module for pages with embedded YouTube video Link to this post

    Hi there,

    Nice work!

    You could probably do something like this on the Page class:

    function Border() {
    if($this->Border) {
    return $this->Border;
    } else {
    return '0';

    Calls to $this->Border can then be replaced with $this->Border()

    0 isn't output, because if it's 0 it's null. So, I guess you have to explicitly pass the 0 on as a string instead.

    The data type for Border should probably be 'Boolean', since it's only 1 or 0 correct? If that's also the case, you could use the field NumericField instead of TextField in getCMSFields() (so you can't input anything apart from a number). One of the arguments to NumericField also allows you to limit the characters for that field with maxlength, so if it's a boolean you can't input anything more than 1 character.


  • orientel
    Community Member
    2 Posts

    Re: New Module for pages with embedded YouTube video Link to this post

    Thanks... it's strange for me that zero character is considered a null value in a Varchar or Text data field, however I understood the point (solution was simpler than expected).

    I fixed and updated my module and it's working fine now (same url of first post, now also autoplay parameter is managed).

    Thanks again for answer, bye!

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.