You are here

function youtubechannelvideo in YoutubeChannel 8.3

Same name and namespace in other branches
  1. 8.2 youtubechannel.module \youtubechannelvideo()

Function to get video from youtube channel.

1 call to youtubechannelvideo()
youtubechannel_theme in ./youtubechannel.module
Implements hook_theme().

File

./youtubechannel.module, line 49
Contains hooks for youtubechannel module.

Code

function youtubechannelvideo() {
  $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');
  $vars = array();

  /**
   * 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}";
  $channeljson = '';
  try {
    $channeljson = (string) \Drupal::httpClient()
      ->get($path, array(
      'verify' => false,
    ))
      ->getBody();
  } catch (RequestException $e) {
  } catch (BadResponseException $e) {
  } catch (\Exception $e) {
  }
  $channel_data = json_decode($channeljson, true);
  $uploads_id = $channel_data['items'][0]['contentDetails']['relatedPlaylists']['uploads'];
  if (!empty($channel_data['items'])) {
    $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}";
    try {
      $playlistjson = (string) \Drupal::httpClient()
        ->get($uri, array(
        'verify' => false,
      ))
        ->getBody();
    } catch (RequestException $e) {
    } catch (BadResponseException $e) {
    } catch (\Exception $e) {
    }
    $feed_array = json_decode($playlistjson, true);
    if ($feed_array['pageInfo']['totalResults'] == 0) {
      return t("No videos available on this channel.");
    }
    $videos = array();
    foreach ($feed_array['items'] as $key => $value) {
      $youtube_id = $value['snippet']['resourceId']['videoId'];
      $title = $value['snippet']['title'];
      $videos[$youtube_id] = $value['snippet']['thumbnails']['default']['url'];

      //$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;
    $url = Url::fromUri('https://www.youtube.com/channel/' . $youtube_channel_id);
    $channelLink = Link::fromTextAndUrl(t('Goto Youtube Channel'), $url);
    $channelLink = $channelLink
      ->toRenderable();
    $channelLink['#attributes'] = array(
      'target' => '_blank',
    );
    $vars['channelLink'] = $channelLink;
  }
  else {
    $vars['show_error'] = true;
    $url = Url::fromRoute('youtubechannel.settings');
    $vars['config_link'] = Link::fromTextAndUrl(t('admin page'), $url)
      ->toString();
  }
  return $vars;
}