You are here

function facebook_comments_box_block in Facebook Comments Box 6

Implements hook_block().

File

./facebook_comments_box.module, line 27
Add Facebook Comments to selected content types on your site.

Code

function facebook_comments_box_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks['facebook_comments_box'] = array(
      'info' => t('Facebook Comments Box'),
    );
    return $blocks;
  }
  else {
    if ($op == 'view') {
      $block = array();
      switch ($delta) {
        case 'facebook_comments_box':
          $fcb_title = '';
          $node_type = '';

          // Get which node types should have comments.
          $fcb_node_types = variable_get('facebook_comments_box_default_node_types', array(
            NULL,
          ));

          // Figure out which node and nodetype we're on, puts comments only on
          // nodes (as opposed to the front page, views pages, term pages, etc.)
          if (arg(0) == 'node' && is_numeric(arg(1))) {
            $nid = arg(1);
            $node = node_load($nid);
            $node_type = $node->type;
            $fcb_title = check_plain($node->title);
          }

          // If the nodetype is set and selected in config, then load the comments
          // on the page.
          if (isset($node_type) && in_array($node_type, $fcb_node_types) && $node->status) {

            // Figure out the current absolute URL.
            $path = isset($_GET['q']) ? $_GET['q'] : '<front>';
            $fcb_url = url($path, array(
              'absolute' => TRUE,
            ));

            // Meta mark up for the page.
            $markup = '<meta property="og:type" content="Article" />';
            $markup .= '<meta property="og:title" content="' . $fcb_title . '"/>';
            $markup .= '<meta property="og:url" content="' . $fcb_url . '"/>';
            $markup .= '<meta property="og:image" content="' . theme_get_setting('logo') . '"/>';
            $markup .= '<meta property="fb:admins" content="' . check_plain(variable_get('facebook_comments_box_admin_id', NULL)) . '">';

            // Add meta data to the HEAD.
            drupal_set_html_head($markup);

            // Set the title of the block.
            $block['subject'] = t('Facebook Comments Box');

            // Set the content of the block.
            $fcb_content = '<div class="facebook-comments-box">';
            $fcb_content .= '<div id="fb-root"></div>';
            $fcb_content .= '<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>';
            $fcb_content .= '<fb:comments href="' . $fcb_url . '" ';
            $fcb_content .= ' num_posts="' . check_plain(variable_get('facebook_comments_box_default_comments', 10)) . '" ';
            $fcb_content .= ' width="' . check_plain(variable_get('facebook_comments_box_default_width', 400)) . '" ';
            $fcb_content .= ' colorscheme="' . variable_get('facebook_comments_box_default_theme', 'light') . '" ></fb:comments></div>';
            $block['content'] = $fcb_content;
          }
          break;
      }
    }
  }
  return $block;
}