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

Commit 30fe49c

Browse files
author
Ed Linklater
committed
Add static method to get video details from ID (fixes #21)
1 parent b14b287 commit 30fe49c

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ view count) and provides information immediately (rather than after saving).
2323
```php
2424
<?php
2525

26+
use EdgarIndustries\YouTubeField\YouTubeField;
27+
2628
class Page extends SiteTree
2729
{
2830

@@ -32,7 +34,7 @@ class Page extends SiteTree
3234

3335
public function getCMSFields() {
3436
$fields = parent::getCMSFields();
35-
$fields->addFieldToTab('Root.Main', new \EdgarIndustries\YouTubeField\YouTubeField('VideoID', 'YouTube Video'));
37+
$fields->addFieldToTab('Root.Main', new YouTubeField('VideoID', 'YouTube Video'));
3638
return $fields;
3739
}
3840

src/YouTubeField.php

+23-18
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,7 @@ public function Field($properties = [])
2626
$this->setAttribute('data-apikey', $api_key);
2727
Requirements::javascript('https://apis.google.com/js/client.js?onload=googleApiClientReady');
2828
} elseif (!empty($this->value) && self::url_parser($this->value)) {
29-
$client = new Client();
30-
try {
31-
$res = $client->get('https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=' . $this->value . '&format=json');
32-
if ($res->getStatusCode() == '200' && $data = json_decode($res->getBody())) {
33-
$api_data = new \stdClass();
34-
$api_data->id = $this->value;
35-
$api_data->snippet = new \stdClass();
36-
$api_data->snippet->title = $data->title;
37-
if (preg_match('/user\/([\w-]+)/', $data->author_url, $author)) {
38-
$api_data->snippet->channelTitle = $author[1];
39-
}
40-
$api_data->snippet->thumbnails = new \stdClass();
41-
$api_data->snippet->thumbnails->default = new \stdClass();
42-
$api_data->snippet->thumbnails->default->url = $data->thumbnail_url;
43-
44-
$this->setAttribute('data-apidata', json_encode($api_data));
45-
}
46-
} catch (ClientException $e) {}
29+
$this->setAttribute('data-apidata', json_encode(static::get_video_information($this->value)));
4730
}
4831

4932
return parent::Field($properties);
@@ -73,6 +56,28 @@ public function Type()
7356
return 'text youtube';
7457
}
7558

59+
public static function get_video_information($videoId)
60+
{
61+
$client = new Client();
62+
try {
63+
$res = $client->get('https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=' . $videoId . '&format=json');
64+
if ($res->getStatusCode() == '200' && $data = json_decode($res->getBody())) {
65+
$api_data = new \stdClass();
66+
$api_data->id = $videoId;
67+
$api_data->snippet = new \stdClass();
68+
$api_data->snippet->title = $data->title;
69+
if (preg_match('/user\/([\w-]+)/', $data->author_url, $author)) {
70+
$api_data->snippet->channelTitle = $author[1];
71+
}
72+
$api_data->snippet->thumbnails = new \stdClass();
73+
$api_data->snippet->thumbnails->default = new \stdClass();
74+
$api_data->snippet->thumbnails->default->url = $data->thumbnail_url;
75+
76+
return $api_data;
77+
}
78+
} catch (ClientException $e) {}
79+
}
80+
7681
/**
7782
* Parse YouTube URL into a valid 11-character ID.
7883
*

0 commit comments

Comments
 (0)