Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
Add static method to get video details from ID (fixes #21)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Linklater committed Oct 13, 2019
1 parent b14b287 commit 30fe49c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ view count) and provides information immediately (rather than after saving).
```php
<?php

use EdgarIndustries\YouTubeField\YouTubeField;

class Page extends SiteTree
{

Expand All @@ -32,7 +34,7 @@ class Page extends SiteTree

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

Expand Down
41 changes: 23 additions & 18 deletions src/YouTubeField.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,7 @@ public function Field($properties = [])
$this->setAttribute('data-apikey', $api_key);
Requirements::javascript('https://apis.google.com/js/client.js?onload=googleApiClientReady');
} elseif (!empty($this->value) && self::url_parser($this->value)) {
$client = new Client();
try {
$res = $client->get('https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=' . $this->value . '&format=json');
if ($res->getStatusCode() == '200' && $data = json_decode($res->getBody())) {
$api_data = new \stdClass();
$api_data->id = $this->value;
$api_data->snippet = new \stdClass();
$api_data->snippet->title = $data->title;
if (preg_match('/user\/([\w-]+)/', $data->author_url, $author)) {
$api_data->snippet->channelTitle = $author[1];
}
$api_data->snippet->thumbnails = new \stdClass();
$api_data->snippet->thumbnails->default = new \stdClass();
$api_data->snippet->thumbnails->default->url = $data->thumbnail_url;

$this->setAttribute('data-apidata', json_encode($api_data));
}
} catch (ClientException $e) {}
$this->setAttribute('data-apidata', json_encode(static::get_video_information($this->value)));
}

return parent::Field($properties);
Expand Down Expand Up @@ -73,6 +56,28 @@ public function Type()
return 'text youtube';
}

public static function get_video_information($videoId)
{
$client = new Client();
try {
$res = $client->get('https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=' . $videoId . '&format=json');
if ($res->getStatusCode() == '200' && $data = json_decode($res->getBody())) {
$api_data = new \stdClass();
$api_data->id = $videoId;
$api_data->snippet = new \stdClass();
$api_data->snippet->title = $data->title;
if (preg_match('/user\/([\w-]+)/', $data->author_url, $author)) {
$api_data->snippet->channelTitle = $author[1];
}
$api_data->snippet->thumbnails = new \stdClass();
$api_data->snippet->thumbnails->default = new \stdClass();
$api_data->snippet->thumbnails->default->url = $data->thumbnail_url;

return $api_data;
}
} catch (ClientException $e) {}
}

/**
* Parse YouTube URL into a valid 11-character ID.
*
Expand Down

0 comments on commit 30fe49c

Please sign in to comment.