Skip to content

Instantly share code, notes, and snippets.

@cmcramer
Created December 15, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmcramer/60b7ff41017b4a633894 to your computer and use it in GitHub Desktop.
Save cmcramer/60b7ff41017b4a633894 to your computer and use it in GitHub Desktop.
<?php
class CmcYouTubeEmbed extends DataObject {
private static $singular_name = 'Embedded YouTube Video';
private static $plural_name = 'Embedded YouTube Videos';
private static $db = array(
'YouTubeTitle' => 'Varchar(255)',
'YouTubeId' => 'Varchar(100)',
'Width' => 'Int',
'Height' => 'Int',
'AutoPlay' => 'Boolean',
'ShowInfo' => 'Boolean',
);
private static $has_one = array(
'PageWithVideo' => 'Page',
);
private static $summary_fields = array(
'YouTubeTitle' => 'YouTube Video',
);
private static $default_sort = array(
'YouTubeTitle',
);
private static $display_fields = array(
'Title' => 'YouTube Video',
'YouTubeId' => 'Video ID',
'AutoPlay.Nice' => 'Auto Play',
'ShowInfo.Nice' => 'Show Info',
);
private static $defaults = array(
'Width' => 640,
'Height' => 360,
'AutoPlay' => true,
'ShowInfo' => false,
);
public function getTitle() {
return $this->YouTubeTitle;
}
public function WatchOnYouTubeUrl() {
$strUrl = "https://www.youtube.com/watch?v={$this->YouTubeId}";
return $strUrl;
}
public function VideoIframe() {
if ($this->AutoPlay) {
$strAutoPlay = '&autoplay=1';
} else {
$strAutoPlay = '&autoplay=0';
}
if ($this->ShowInfo) {
$strShowInfo = '&showinfo=1';
} else {
$strShowInfo = '&showinfo=0';
}
$strHtml = <<<EOT
<iframe width="{$this->Width}" height="{$this->Height}"
src="https://www.youtube.com/embed/{$this->YouTubeId}?rel=0{$strAutoPlay}{$strShowInfo}"
frameborder="0"></iframe>
EOT;
return $strHtml;
}
}
class VideoPage extends Page {
...
private static $has_many = array(
'Webcams' => 'CmcYouTubeEmbed',
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldsToTab('Root.Webcams', GridField::create(
'Webcams',
'Webcams',
$this->Webcams(),
GridFieldConfig_RecordEditor::create()
));
return $fields;
}
}
?>
/* In the template */
<div id="webcams">
<% loop $Webcams %>
<span class="webcam-block">
$VideoIframe
<p>$YouTubeTitle</p>
</span><!-- .webcam-block -->
<% end_loop %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment