SilverStripe Field for selecting YouTube videos. Takes various URL formats as input and stores the YouTube ID
PHP JavaScript CSS Scheme
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
code
css
images
javascript
templates
tests
.editorconfig
.gitattributes
.scrutinizer.yml
.travis.yml
LICENSE.md
README.md
_config.php
code-of-conduct.md
composer.json

README.md

YouTube Field for SilverStripe

Introduction

This SilverStripe module provides a simple YouTube field, primarily for use in the CMS/ModelAdmin. It accepts input of various common YouTube URL formats and converts them for storage in database as the 11-character YouTube ID.

Requirements

  • silverstripe/framework 3.0+ for basic field and URL parser
  • silverstripe/framework 3.3+ for video information support

Basic field

private static $db = array(
    'VideoID' => 'Varchar(11)',
);

public static getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Main', new YouTubeField('VideoID', 'YouTube Video'));
    return $fields;
}

Video Information

Optionally you can provide a key for the YouTube v3 API, which is used to display video information (title, thumbnail, length) under the field when a valid ID has been provided.

Screenshot of Video Information

mysite/_config/youtubefield.yml

---
name: youtubefield
---
YouTubeField:
  api_key: YOUR_API_KEY

URL Parser

There is a static function which can be called (without using the YouTubeField) to simply retrieve the YouTube ID from a supported URL format.

YouTubeField::url_parser($url);