Better, Stronger, Faster youtubebadge

Last modified: March 26, 2008 - 19:34

I removed the requirement to include the Zend library separately. I added it to the module directory. This is what I ended up with. Considering adding this as a module.. but still dont like the included Zend library files.

<?php
ini_set
('include_path', ini_get('include_path').':'.drupal_get_path("module", "youtubebadge"));

/**
* 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 <a href="http://code.google.com/apis/youtube/developers_guide_php.html#YouTubeService
" title="http://code.google.com/apis/youtube/developers_guide_php.html#YouTubeService
" rel="nofollow">http://code.google.com/apis/youtube/developers_guide_php.html#YouTubeSer...</a> * Also, if you get a blank page, check the http error log, you might not be able to find DomDocument
* class if PHP was not complied with Dom support, installing the php-xml rpm fixed that issue.  You
* could also recompile PHP without the --deisable-dom switch.
*/

/**
* Implementation of hook_requirements
* Check for the zend gdata library
*/
function youtubebadge_requirements($phase) {
 
$requirements = array();
 
$t = get_t();

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

  switch (
$phase) {
    case
"install":
      if(!
$gdata_installed) {
       
$requirements['youtubebadge'] = array(
         
'title' => $t('YouTube Badge'),
         
'value' => $t('Download and extract gdata library.'),
         
'severity' => REQUIREMENT_ERROR,
        );
      }
    case
"runtime":
     
$requirements['jquery'] = array(
         
'title' => $t('YouTube Badge'),
         
'severity' => REQUIREMENT_OK,
         
'value' => $t('Zend gdata library installed correctly for YouTube Badge.'),
        );
    }

  return
$requirements;
}

/**
* 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"
      );

      $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();
     
//$block['content'] ="test";

     
return $block;
  }
}

// contents of the block
function youtubebadge_block_contents() {
 
$output = "";
  include_once
'Zend/Loader.php';

 
Zend_Loader::loadClass('Zend_Gdata_YouTube');

  if(
class_exists("Zend_Gdata_YouTube")) {
   
$yt = new Zend_Gdata_YouTube();

  
$feed_url = variable_get('youtube_feed_url_text', FALSE);
  
$video_feed = $yt->getVideoFeed($feed_url);

  
$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", $video_feed, $block_args);
  
//$output .= theme("badge_block", null , $block_args);
 
} else
   
$output .= theme("badge_block", NULL);

return
$output;
}

/**
* Themeing function for youtube thumbnail badge
*
*/
function theme_badge_block($video_feed, $block_args = array('images-wide'=>1)) {

 
drupal_add_css(drupal_get_path('module', "youtubebadge") . "/youtubebadge.css", "module");
 
$images = array();

  if(
$video_feed) {
    foreach (
$video_feed as $videoEntry) {
     
$images[] = theme("video_entry", $videoEntry, $block_args );
    }
  } else {
   
$images[] = "<div>Feed not available.</div>";
  }

 
$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;
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.