You are here

youtubechannel.module in YoutubeChannel 8

Contains gamabhana.module.

File

youtubechannel.module
View source
<?php

/**
 * @file
 * Contains gamabhana.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Render\HtmlResponseAttachmentsProcessor;
use Drupal\Core\Http\ClientFactory;
use Drupal\Component\Serialization\Json;
use Drupal\vendor\guzzlehttp\guzzle\src\Exception;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\Component\Utility\String;
use Drupal\image\Entity\ImageStyle;

/**
 * Implements hook_page_top().
 */
function youtubechannel_page_top(array &$page_top) {
  $page_top['youtubechannel'] = [
    '#attached' => [
      'library' => [
        'youtubechannel/youtubechannel',
      ],
    ],
  ];
}

/**
 * Implements hook_theme().
 */
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());
  }
}

//http://drupal.stackexchange.com/questions/193540/drupal-8-custom-block-with-html-and-javascript

Functions