Problem/Motivation

Hi, I needed to control facebook's comments display and found they are printed inside node's content making them unavailable to display using contexts or drupal's core block system.

Proposed resolution

Add a block to control display of FBC (Facebook Comments) as blocks in addition to the content type/per node basis system.

Remaining tasks

Maybe somthing like the following would be enough:


//Create the block content reusing code used in facebook_comments_node_view
function _facebook_comments_block_content() {
  // Add the Facebook App ID if it exists
  if ($appid = variable_get('facebook_comments_appid', '')) {    
    $element = array(
      '#tag' => 'meta',
      '#attributes' => array(
        'property' => 'fb:app_id',
        'content' => $appid,
      ),
    );
    drupal_add_html_head($element, 'facebook_comments');
  }
  global $base_url;
  $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
  $protocol = strtolower($_SERVER["SERVER_PROTOCOL"]);
  $protocol = substr($protocol, 0, strpos($protocol, "/")) . $s; 
  $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); 
  $url = $base_url .'/'. drupal_get_path_alias($_GET['q']);

  $width = variable_get('facebook_comments_width', '620');
  $style = variable_get('facebook_comments_style', 'light');

 //We should also add an extra field to configure the ammount of comments to show in the block
 //See facebook_comment_admin below 
  $comments->ammount = variable_get('facebook_comments_ammount','15');

  $facebook_comments = '<div id="fb-root"></div>
  <script>(function(d, s, id) {
		var js, fjs = d.getElementsByTagName(s)[0];
		if (d.getElementById(id)) {return;}
		js = d.createElement(s); js.id = id;
		js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
		fjs.parentNode.insertBefore(js, fjs);
  }(document, "script", "facebook-jssdk"));</script>
  <div class="fb-comments" data-href="'. $url .'" data-num-posts="'. $comments->amount .'" data-width="'. $width .'" data-colorscheme="'.$style.'"></div>';
  $output = $facebook_comments;
  return $output;
}
//Declare block
function facebook_comments_block_info() {
  $blocks['fbc'] = array(
    'info' => t('Facebook comments anywhere'),
    'cache' => DRUPAL_NO_CACHE
  );
  return $blocks;
}
//Build block
function facebook_comments_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'fbc':
        $block['content'] = _facebook_comments_block_content();
      }
  return $block;
}

We should add the variable facebook_comment_ammount in facebook_comments_admin.


function facebook_comments_admin() {
  $form = array();
  $form['facebook_comments_appid'] = array(
    '#type' => 'textfield',
    '#title' => t('Facebook App ID'),
    '#default_value' => variable_get('facebook_comments_appid', ''),
    '#description' => t('Enter the Facebook App ID to ensure that all comments can be grouped for moderation.'),
  );
  $form['facebook_comments_width'] = array(
    '#type' => 'textfield',
    '#title' => t('Facebook comment plugin width'),
    '#default_value' => variable_get('facebook_comments_width', '620'),
    '#description' => t('The width of the Facebook comment plugin width, in pixels. Example: 620'),
  );
  $form['facebook_comments_style'] = array(
    '#type' => 'select',
    '#title' => t('Color Scheme'),
    '#default_value' => variable_get('facebook_comments_style', 'light'), 
    '#options' => array('light' => t('Light'), 'dark' => t('Dark')),
  );
  $form['facebook_comments_ammount'] = array(
    '#type' => 'integer',
    '#title' => t('Ammount of comments in FBC(Facebook comments) block'),
    '#default_value' => variable_get('facebook_comments_ammount', '15'), 
  );
  $defaulttypes = array();
  $types = node_type_get_types();
  foreach ($types as $key => $type) {
    $defaulttypes[$key] = $type->name;
  }
  $form['facebook_comments_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Facebook comment default content types'),
    '#options' => $defaulttypes,
    '#default_value' => variable_get('facebook_comments_types', array()),
    '#description' => t('Check the content types that should have Facebook comments enabled by default.'),
  );
  $form['facebook_comments_applyall'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable Facebook comments on existing content for the selected content types.'),
    '#default_value' => FALSE,
  );
  $form['#submit'][] = 'facebook_comments_admin_applyall';
  return system_settings_form($form);
}

Comments

wmostrey’s picture

Title: Adding a facebook comments block » Adding a Facebook Comments block
Status: Needs review » Needs work

This is actually a really great idea! The patch needs a bit of work though in order to reuse more code instead of duplicating it. I'll get around this myself in a few days.

wmostrey’s picture

Status: Needs work » Fixed

This block was added in the latest dev release. The width and amount of comments can be controlled on the configuration page.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Maglor’s picture

I'm having an issue with this block. It seems like it's not sorting per node. When someone places a FB comment in one node, this one is visible everywhere and same goes with replies etc. It has become one big, comment fields applicable for every node.