Started out just using this simple script, http://www.flashandburn.net/youtubeBadge/ , but quickly realized that I couldn't get the presentation exactly the way I wanted it so I turned to the YouTube Data API and never looked back.

So how do we get Drupal to use the Google Data PHP Client Library?

Start with Google's straighforward installation directions. The biggest step in all this is putting the gdata library on the PHP path.
This is easily done in your Drupal site's settings.php file.

ini_set('include_path', ini_get('include_path').':/path/to/gdata/libs');

Basically, its the parent directory of the Zend folder that was under the library directory from the ZendGdata tar file.

Now, to check if the php changes took hold, use the Devel module to view phpinfo (admin/logs/status/php).

Here's the code for the module. Simply put, this is just a custom block that grabs a YouTube video feed and displays the entries in a badge format. I figured it would be better suited here since gdata needs to be configured separately.

require_once 'Zend/Loader.php';
/**
 * Zend gdata libraries had to be added to the php page.  I did this inside settings.php
 * by adding ' ini_set('include_path', ini_get('include_path').':path-to-zend-libraries'); '
 * Got help from http://code.google.com/apis/youtube/developers_guide_php.html#YouTubeService
 *
 */

/**
 * Implementation of hook_block()
 *
 */
function youtubebadge_block($op = 'list', $delta = 0, $edit = array()) {
  switch($op) {
    case 'list' :
      $blocks[0]['info'] = t("YouTube Video Badge");
      return $blocks;

    case 'configure':
      $form = array();
      $form['youtube_feed_url_text'] = array(
        '#type' => 'textfield',
        '#title' => t('YouTube Feed URL'),
        '#size' => 90,
        '#default_value' => variable_get('youtube_feed_url_text',
                              "http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?max-results=9"),
        '#description' => t('See the <a target="_blank" 
          href="http://code.google.com/apis/youtube/reference.html">YouTube Data API Reference</a> on how to create a feed URL'),
      );

      $form['youtube_feed_rss_url'] = array(
        '#type' => 'textfield',
        '#size' => 90,
        '#default_value' => variable_get('youtube_feed_rss_url', ""),
        '#description' => t('The URL for the rss link on the block title bar.'),
      );

      $form['youtube_block_title_url'] = array(
        '#type' => 'textfield',
        '#size' => 90,
        '#default_value' => variable_get('youtube_block_title_url', ""),
        '#description' => t('The URL for the block title link.'),
      );

      $form['youtube_row_width'] = array(
        '#type' => 'textfield',
        '#title' => 'Row Width',
        '#default_value' => variable_get('youtube_row_width', 200),
        '#description' => t('An approximate width in pixels that the badge has to display '.
                            'one row of thumbnails.<br/>This value helps determine the thumbnail size.'),
      );

      $form['youtube_images_wide'] = array(
        '#type' => 'textfield',
        '#title' => 'Entries Across',
        '#default_value' => variable_get('youtube_images_wide', 1),
        '#description' => t('How many thumbnails across the top of the badge.'),
      );

      // add URL test button
      return $form;

    case 'save':
      // only one block so we don't need delta switch
      variable_set('youtube_feed_url_text', $edit['youtube_feed_url_text']);
      variable_set('youtube_feed_rss_url', $edit['youtube_feed_rss_url']);
      variable_set('youtube_block_title_url', $edit['youtube_block_title_url']);
      variable_set('youtube_row_width', $edit['youtube_row_width']);
      variable_set('youtube_images_wide', $edit['youtube_images_wide']);
      return;

    case 'view': default:
      $block['subject'] = t('YouTube Video Badge');
      $block['content'] = youtubebadge_block_contents();

      return $block;
  }
}

// contents of the block
function youtubebadge_block_contents() {
  Zend_Loader::loadClass('Zend_Gdata_YouTube');
  $yt = new Zend_Gdata_YouTube();

  $feed = variable_get('youtube_feed_url_text', FALSE);

  if($feed) {
    $block_args = array(
      'available-width' => variable_get('youtube_row_width', 0),
      'images-wide' => variable_get('youtube_images_wide', 1),
      'rss-url' => variable_get('youtube_feed_rss_url', ""),
      'title-url' => variable_get('youtube_block_title_url', ""),
    );

    $output .= theme("badge_block", $yt->getVideoFeed($feed), $block_args);
  }
  return $output;
}



/**
 * Themeing function for youtube thumbnail badge 
 *
 */
function theme_badge_block($video_feed, $block_args = array()) {
  drupal_add_css(drupal_get_path('module', "youtubebadge") . "/youtubebadge.css", "module");
  $images = array();
  foreach ($video_feed as $videoEntry) {
    $images[] = theme("video_entry", $videoEntry, $block_args );
  }

  $rows = array_chunk($images, $block_args['images-wide']);

  $output = "<div id='youtube-badge-block'>";
  foreach($rows as $row) {
    $output .= "<div class='youtube-entry-row'>".implode("", $row)."</div>\n";
  }

  $output .= "</div>";
  return $output;
}

/**
 * Themeing function for video thumbnail entreis
 *
 */
function theme_video_entry($videoEntry, $block_args = array()) {
  $thumb = $videoEntry->mediaGroup->thumbnail[0];

  $sugg_width = floor($block_args['available-width'] / $block_args['images-wide']);
  if($sugg_width > 0){
    $output .= "<a href='". $videoEntry->mediaGroup->player[0]->url .
      "' target='_blank'><img class='youtube-thumb' src='$thumb->url' width='$sugg_width'></a>";
  }
  else
    $output .= "<div class='youtube-video-entry'><img class='youtube-thumb' src='$thumb->url'></div>";

  return $output;
}

So after enabling the module and going to the blocks listing view, you can configure your YouTube Badge Block. Check out the the configure screen and you can see that the only necessary item is the feed url which is very configurable via feed arguments. I'm using the Row Width and Images Across fields to make a guess about how big the thumbnails should be.

Good Luck!

Comments

emackn’s picture

I found this when moving the code to a new server:
After getting
PHP Fatal error: Class 'DOMDocument' not found in /usr/share/php/Zend/Gdata/App.php on line 312
I discovered that my version of php was compiled with the "--disable-dom" switch. My fix was to add the php-xml rpm, but I guess you also recompile PHP.

emackn’s picture

I also added hook_menu

function youtubebadge_menu($may_cache) {
  $items = array();
  if($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/youtubebadge',
      'title' => t("YouTube Badge"),
      'description' => t("set the Zend gdata path"),
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
      'callback' => 'drupal_get_form',
      'callback arguments' => array('youtubebadge_admin_settings')
    );
  }
  return $items;
}

function youtubebadge_admin_settings() {
  $form['youtubebadge_gdata_path'] = array(
    '#type' => 'textfield',
    '#title' => t('Zend gtata library location'),
    '#default_value' => variable_get('youtubebadge_gdata_path', "/usr/share/php"),
    '#size' => 80,
    '#required' => TRUE,
    '#description' => t("Please use an absolute path without a trailing slash '/'. The Zend directory should be a chil
d of the directory above.")
  );
  return system_settings_form($form);
}

And hook_requirements

function youtubebadge_requirements($phase) {
  $requirements = array();
  $t = get_t();

  // check init settings
  // check lib installed
  $gdata_path = variable_get("youtubebadge_gdata_path", "") . "/Zend/Loader.php";
  $gdata_installed = file_exists($gdata_path);

  switch ($phase) {
    case "runtime":
    case "install":
      if(!$gdata_installed) {
        $requirements['youtubebadge'] = array(
          'title' => $t('Zend gdata library is not found'),
          'value' => $t('Download and extract gdata library.'),
          'severity' => REQUIREMENT_ERROR,
          'description' => $t('The YouTube badge module will not work without the Zend gdata library. Please update th
e <a href="/admin/settings/youtubebadge">YouTube Badge admin settings</a>.  The library can be found <a href="http://f
ramework.zend.com/download/gdata">here</a> and you can also check the <a href="http://drupal.org/node/200994">YouTube 
gdata HOWTO page</a> for more help.')
        );
      }
    }

  return $requirements;
}

Checking to see if the Zend/Loader exists might be the wrong thing to check, but I wanted to check for something that I knew was needed.

I also changed the "required_once" call to "include_once" so if the Loader wasn't found, the file would continue to execute. But doing this meant that I had to add other checks to be sure necessary files were available.