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();
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->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 →</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) {
break;
}
} // 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.