Sign up ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

The hook_block() code is the following.

   // …      
   case 'view':
      $url = 'http://example.com/feed'; //the site's rss url
      $blocks['subject'] = t('on the bookshelf');
      $blocks['content'] = _mymodule_fetch_feed($url);
      return $blocks;
   // …

function _mymodule_fetch_feed($url,$num_itmes=3){
  $http_result = drupal_http_request($url);

  if ($http_result->code == 200) {
      $doc = simplexml_load_string($http_result->data);
      if ($doc === FALSE) {
        $msg = "error parsing booksheld 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 _mymodule_block_content($doc, $num_items);
    }
    else {
      $msg = 'No content from %url.';
      $vars = array('%url' => $url);
      watchdog('goodreads', $msg, $vars, WATCHDOG_WARNING);

      return t("The bookshelf is not accessible.");           
    }
  }
}

function _mymodule_block_content($doc, $num_items = 3) {
  $items = $doc->channel->item;
  $count_items = count($items);
  $len = ($count_items < $num_items) ? $count_items : $num_items;
  $template = '<div class="goodreads-item">'.'<img src="%s"/><br/>%s<br/>by%s</div>'; // Default image: 'no cover'.
  $default_img = 'http://www.example.com/images/nocover-60x80.jpg';
  $default_link = 'http://example.com/feed';
  $out = '';
  foreach ($items as $item) {  
    $author = check_plain($item->author_name);
    $title = strip_tags($item->title);
    $link = check_url(trim($item->link));
    $img = check_url(trim($item->book_image_url));

    if (empty($author)) {
      $author = '';
    }

    if (empty($title)) {
      $title = '';
    }
    if (empty($link)!== 0) { 
      $link = $default_link;
    }
    if (empty($img)) {
      $img = $default_img;
    }

    $book_link = l($title, $link);
    $out .= sprintf($template, $img, $book_link, $author);
  }

  $out .= '<br/><div class="goodreads-more">'. l('Goodreads.com', http://example.com').'</div>';
  return $out;
}

I put this code in my module; it can get the data, but the links are all the same.(http://example.com). How can I make it work?

share|improve this question

1 Answer 1

if (empty($link)!== 0) { 
  $link = $default_link;
}

That condition makes no sense. empty() never returns 0, it return TRUE or FALSE. Just use if (empty($link)) {.

Also, you probably want to use the Aggregator, or Feeds module.

share|improve this answer
    
I agree; strict comparison should be avoided when non necessary, and when it causes the control statement to never be executed. I would also use theme('image') to output the HTML for an image. –  kiamlaluno Mar 27 '11 at 15:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.