Custom Block Content with HTML and PHP I am having a lot of trouble understanding what is wrong with my code. I am trying to create a custom Facebook block, utilizing the Facebook Graph API. My module works to the extent that I see some posts show but the following is taking place:

1) The posts duplicate. This does not happen when recreating the feed outside of a drupal module. 

2) Output of Block content is outside of any region and loads at the top of the page. 

Here is the code I am using:

/**
 * Implementation of hook_block_info().
 */
function mymodule_block_info() {
  $blocks = array();
  $blocks['facebook_block'] = array(
    'info' =&gt; t('Facebook Block'),
  );
  return $blocks;
}

/**
 * Implementation of hook_block_view().
 */
function mymodule_block_view($delta='') {
  $block = array();
  switch ($delta) {
    case 'facebook_block':
      $block['subject'] = '';
      $block['content'] = facebook_main_contents();
      break;
  }
  return $block;
}

function facebook_main_contents(){
require_once('resources/facebook-php-sdk-master/src/facebook.php');

// connect to app
$config = array();
$config['appId'] = 'MYAPPID';
$config['secret'] = 'MYSECRETID';
$config['fileUpload'] = false; // optional

// instantiate
$facebook = new Facebook($config);

// set page id
$pageid = "MYPAGEID";

// now we can access various parts of the graph, starting with the feed
$pagefeed = $facebook-&gt;api("/" . $pageid . "/feed");

echo "&lt;div class=\"fb-feed\"&gt;";

// set counter to 0, because we only want to display 10 posts
$i = 0;
foreach($pagefeed['data'] as $post) {


    if ($post['type'] == 'link' || $post['type'] == 'photo') {


        // open up an fb-update div
        echo "&lt;div class=\"fb-update\"&gt;";

            // check if post type is a link
            if ($post['type'] == 'link') {
                echo "&lt;p&gt;" . $post['message'] . "&lt;/p&gt;";
                echo "&lt;a class=\"container\" href=\"" . $post['link'] . "\" target=\"_blank\"&gt;&lt;div class=\"sharedlink\"&gt;";
                echo "&lt;div class=\"image\"&gt;&lt;img src=\" " . $post['picture'] . "\"/&gt;&lt;/div&gt;";
                echo "&lt;p&gt;" . $post['name'] . "&lt;/p&gt;";
                echo "&lt;/div&gt;&lt;/a&gt;";
                echo "&lt;div class=\"posted\"&gt;Posted on: " . date("jS M, Y", (strtotime($post['created_time']))) . "&lt;/div&gt;";
            }

            // check if post type is a photo
            if ($post['type'] == 'photo') {
                echo "&lt;h2&gt;Photo posted on: " . date("jS M, Y", (strtotime($post['created_time']))) . "&lt;/h2&gt;";
                if (empty($post['story']) === false) {
                    echo "&lt;p&gt;&lt;h2&gt;Story&lt;/h2&gt;" . $post['story'] . "&lt;/p&gt;";
                } elseif (empty($post['message']) === false) {
                    echo "&lt;p&gt;&lt;h2&gt;Message&lt;/h2&gt;" . $post['message'] . "&lt;/p&gt;";
                }
                echo "&lt;p&gt;&lt;a href=\"" . $post['link'] . "\" target=\"_blank\"&gt;View photo &amp;rarr;&lt;/a&gt;&lt;/p&gt;";
            }


        echo "&lt;/div&gt;"; // close fb-update div

        $i++; // add 1 to the counter if our condition for $post['type'] is met
    }


    //  break out of the loop if counter has reached 10
    if ($i == 5) {
        break;
    }
} // end the foreach statement

echo "&lt;/div&gt;";
}


I have tried placing my content code in a separate php file and including it, but that just breaks everything.
The problem is the lack of a return value in facebook_main_contents(); As you can see in the api documentation $block['content'] should contain either a Drupal render array or fully rendered html. facebook_main_contents(); is not actually returning anything but instead echoing output to the page.

You need to collect your rendered html and return it, like so:

$content = "&lt;div class=\"fb-feed\"&gt;";
// check if post type is a link
if ($post['type'] == 'link') {
  $content .= "&lt;p&gt;" . $post['message'] . "&lt;/p&gt;";
  $content .= "&lt;a class=\"container\" href=\"" . $post['link'] . "\" target=\"_blank\"&gt;&lt;div class=\"sharedlink\"&gt;";
  $content .= "&lt;div class=\"image\"&gt;&lt;img src=\" " . $post['picture'] . "\"/&gt;&lt;/div&gt;";
  $content .= "&lt;p&gt;" . $post['name'] . "&lt;/p&gt;";
  $content .= "&lt;/div&gt;&lt;/a&gt;";
  $content .= "&lt;div class=\"posted\"&gt;Posted on: " . date("jS M, Y", (strtotime($post['created_time']))) . "&lt;/div&gt;";
}
...

return $content;