It's not easy for a newbie to follow along with this book when the errata is NOT changed in the download code http://www.packtpub.com/drupal-6-module-development/book. I have send a message about this and hope the code will be changed.

Does any of you have the CORRECT code for Module goodreads for me to use, please?

But have to say it's a great book and easier to understand for a newbie also to php than the book PRO Drupal Development which I will think I understand better after reading this one.

Edited 16:40: It's the errata on page 45

Comments

mbutcher’s picture

I'm not sure how to read your comment at the end of your post. Did the errata on page 45 solve the problem, or do you still need a solution?

tinem’s picture

I still need YOU to make ALL the changes for the errata to the download code, please.

tinem’s picture

This is directly from the download code for goodreads.module:

<?php
// $Id$

/**
 * @file
 * Module for fetching data from GoodReads.com.
 * This module provides block content retrieved from a GoodReads.com
 * bookshelf.
 * @see http://www.goodreads.com
 */

/**
 * Implementation of hook_block
 */
function goodreads_block($op='list' , $delta=0, $edit=array()) {
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('Goodreads Bookshelf');
      return $blocks;
    case 'view':
      $url = 'http://www.goodreads.com/review/list_rss/'
            .'398385'
            .'?shelf='
            .'history-of-philosophy';
      $blocks['subject'] = t('On the Bookshelf');
      $blocks['content'] = _goodreads_fetch_bookshelf($url);
      return $blocks;
  }
}

/**
 * Implementation of hook_help()
 */
function goodreads_help($path, $arg) {
  
  if ($path == 'admin/help#goodreads') {
    $txt = 'The Goodreads module uses the !goodreads_url XML '
      .'API to retrieve a list of books and display it as block '
      .'content.';
    $link = l('Goodreads.com', 'http://www.goodreads.com');
    $replace = array(
      '!goodreads_url' => $link
    );
    return '<p>'. t($txt, $replace) .'</p>';
    
  }
}

/* END HOOKS */


/**
 * Retrieve information from the Goodreads bookshelp XML API.
 * 
 * This makes an HTTP connection to the given URL, and retrieves
 * XML data, which it then attempts to format for display.
 *
 * @param $url URL to the goodreads bookshelf.
 * @param $num_items Number of items to include in results.
 * @return string String containing the bookshelf.
 */
function _goodreads_fetch_bookshelf($url, $num_items=3) {
  $http_result = drupal_http_request($url);
  if ($http_result->code == 200) {
      $doc = new DOMDocument();
      try {
        $doc->loadXML($http_result->data);
      } catch (DOMException $e) {
        $msg = "Error parsing bookshelf XML for %url: %msg.";
        $vars = array('%url'=>$url, '%msg'=>$e->getMessage());
        watchdog('goodreads', $msg, $vars, WATCHDOG_WARNING);
        return t("Getting the bookshelf resulted in an error."); 
      }
      
      return _goodreads_block_content($doc, $num_items);
      
    // Otherwise we don't have any data
  } else {
      $msg = 'No content from %url.';
      $vars = array( '%url' => $url );
      watchdog('goodreads', $msg, $vars, WATCHDOG_WARNING);
      return t("The bookshelf is not accessible.");
  }
}


/**
 * Generate the contents of a block from a DOM document.
 * Given a DOM document and the maximum number of entries,
 * generate some content.
 * @param $doc DOMDocument object containing Goodreads XML.
 * @param $num_items Number of items to format for display.
 * @return string Formatted string. 
 */
function _goodreads_block_content($doc, $num_items=3) {
  
  $items_nl = $doc->getElementsByTagName('item');
  $len = ($items_nl->length < $num_items) ? $items_nl->length : $num_items;
  
  $template = '<div class="goodreads-item">'
            .'<img src="%s"/><br/>%s<br/>by %s</div>';
  
  for ($i = 0; $i < $len; ++$i) {
    
    $item_ele = $items_nl->item($i);
    $children_nl = $item_ele->childNodes;
    
    // Default image: 'no cover'
    $img = 'http://www.goodreads.com/images/nocover-60x80.jpg';
    $link = 'http://www.goodreads.com';
    $author = '';
    $title = '';
    
    foreach ($children_nl as $child_node) {
      
      if ($child_node->nodeType == XML_ELEMENT_NODE) {
        switch ($child_node->nodeName) {
          
          case 'author_name':
            if (strlen($author) > 0) $author .= ', ';
            $author .= check_plain($child_node->textContent);
            break;
            
          case 'book_image_url':
            $img = check_plain($child_node->textContent);
            break;
            
          case 'title':
            $title = $child_node->textContent;
            break;
            
          case 'link':
            $link = check_plain($child_node->textContent);
            break;
        }
      }
    }
    
    $book_link = l($title, $link);
    $out .= sprintf( $template, $img, $book_link, $author);
  }
  $out .= '<div class="goodreads-more">'
         . l('Goodreads.com', 'http://www.goodreads.com')
         .'</div>';
  return $out;
}
tinem’s picture

I have installed it on 2 different sites with the code downloaded and without making any changes. Try clicking on the links from the Bookshelf and you will see that they are not returned to the originally sites. Isn't that what it's supposed to do?

http://www.tinemuller.dk/Learning_Drupal_6_Module_Development/node

http://www.test.findtoilet.dk/

mbutcher’s picture

Since I wrote the book, some of the details of GoodReads' API have changed. To accomodate this, try these few minor changes:

Find this...

  $link = check_plain($child_node->textContent);

Change it to this:

  $link = $child_node->textContent;

Find this...

$book_link = l($title, $link);

Change it to this:

$book_link = l($title, $link, array('absolute' => TRUE));
tinem’s picture

I have made the changes but there are still problems. Can someone help, please?

http://www.tinemuller.dk/Learning_Drupal_6_Module_Development/

tinem’s picture

Think I have the same problem as described here http://drupal.org/node/481578 but don't know how to solve it? Shouldn't it have been updated in the core version Drupal v. 6.17?