function youtubechannel_theme in YoutubeChannel 8
Same name and namespace in other branches
- 8.3 youtubechannel.module \youtubechannel_theme()
- 8.2 youtubechannel.module \youtubechannel_theme()
- 6 youtubechannel.module \youtubechannel_theme()
- 7.2 youtubechannel.module \youtubechannel_theme()
- 7 youtubechannel.module \youtubechannel_theme()
Implements hook_theme().
File
- ./
youtubechannel.module, line 33 - Contains gamabhana.module.
Code
function youtubechannel_theme() {
$max_results = 5;
$youtubechannel_config = \Drupal::config('youtubechannel.settings');
$api_key = $youtubechannel_config
->get('youtubechannel_api_key');
$youtube_id = $youtubechannel_config
->get('youtubechannel_id');
$youtube_channel_id = $youtubechannel_config
->get('youtubechannel_id');
$max_results = $youtubechannel_config
->get('youtubechannel_video_limit');
/**
* NEW API v3 feed
*/
// First, let's fetch the channel feed to get the upload playlist.
$path = "https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id={$youtube_id}&maxResults=1&fields=pageInfo/totalResults,items/contentDetails/relatedPlaylists/uploads&key={$api_key}";
try {
$channeljson = (string) \Drupal::httpClient()
->get($path)
->getBody();
if (empty($channeljson)) {
return FALSE;
}
else {
$channel_data = json_decode($channeljson, true);
$uploads_id = $channel_data['items'][0]['contentDetails']['relatedPlaylists']['uploads'];
// Now we have the uploads feed ID, let's grab the the actual video feed.
$uri = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&}&maxResults={$max_results}&fields=pageInfo/totalResults,items/snippet(resourceId/videoId,title,thumbnails/default/url)&playlistId={$uploads_id}&key={$api_key}";
$playlistjson = (string) \Drupal::httpClient()
->get($uri)
->getBody();
if (empty($playlistjson)) {
return FALSE;
}
else {
$feed_array = json_decode($playlistjson, true);
if ($feed_array['pageInfo']['totalResults'] == 0) {
//return t("Sorry, there are no videos available on this channel.");
}
else {
// Now let's iterate through our items.
$videos = array();
foreach ($feed_array['items'] as $key => $value) {
$youtube_id = $value['snippet']['resourceId']['videoId'];
$title = $value['snippet']['title'];
$image_variables = array(
'uri' => $value['snippet']['thumbnails']['default']['url'],
'alt' => $title,
'title' => $title,
);
$thumb = $value['snippet']['thumbnails']['default']['url'];
$videos[$youtube_id] = $thumb;
//$videos[$youtube_id]['title'] = $title;
}
$vars['width'] = [
'#plain_text' => $youtubechannel_config
->get('youtubechannel_video_width'),
];
$vars['height'] = [
'#plain_text' => $youtubechannel_config
->get('youtubechannel_video_height'),
];
$vars['content'] = $videos;
return [
'youtubechannel_block' => [
// Here you can pass any variables you want, if necessary.
'variables' => [
'youtube_content' => $vars,
],
],
];
}
}
}
} catch (RequestException $e) {
var_dump($e
->getMessage());
}
}