diff --git a/README.md b/README.md index 9f0ffde..01a2380 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ view count) and provides information immediately (rather than after saving). ```php addFieldToTab('Root.Main', new \EdgarIndustries\YouTubeField\YouTubeField('VideoID', 'YouTube Video')); + $fields->addFieldToTab('Root.Main', new YouTubeField('VideoID', 'YouTube Video')); return $fields; } diff --git a/src/YouTubeField.php b/src/YouTubeField.php index 7ae68fb..db8019b 100644 --- a/src/YouTubeField.php +++ b/src/YouTubeField.php @@ -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); @@ -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. *