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' => 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();
  return $block;

function facebook_main_contents(){

// 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->api("/" . $pageid . "/feed");

echo "<div class=\"fb-feed\">";

// 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 "<div class=\"fb-update\">";

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

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

        echo "</div>"; // 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) {
} // end the foreach statement

echo "</div>";

I have tried placing my content code in a separate php file and including it, but that just breaks everything.

2 Answers 2


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

return $content;
  • Ahhh, Thanks! I will give this a try on Monday but that makes sense. Will mark this if I succeed but I think this will do it. Apr 17, 2014 at 21:19

Your function facebook_main_contents should return the HTML code. Replace all your "echo" by a variable which will contain your HTML.

In fact, in drupal, you should never use an echo or a print in a file other than a template (*.tpl.php in drupal).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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