You are here

fb_social_comments.module in Facebook social plugins integration 6

File

modules/fb_social_comments/fb_social_comments.module
View source
<?php

/**
 * Implementation of hook_theme
 */
function fb_social_comments_theme() {
  return array(
    'fb_social_comments_block_comments_view' => array(),
  );
}

/**
 * Implementation of hook_menu
 */
function fb_social_comments_menu() {
  $items = array();
  $items['admin/settings/fb/social/comments'] = array(
    'type' => MENU_LOCAL_TASK,
    'title' => 'comments',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'fb_social_comments_settings_form',
    ),
    'file' => 'fb_social_comments.admin.inc',
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  return $items;
}

/**
 * Implementation of hook_block
 */
function fb_social_comments_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks['comments'] = array(
        'info' => t('fb comments'),
      );
      return $blocks;
    case 'view':
      switch ($delta) {
        case 'comments':

          // show the block comment only for nodes
          $node = menu_get_object();
          if (is_object($node) && $node->status && $node->nid && arg(2) != "edit") {
            return array(
              'subject' => t(''),
              'content' => theme('fb_social_comments_block_comments_view', fb_social_url('node/' . $node->nid)),
            );
          }
      }
      break;
  }
}

/**
 * Implementation of hook_content_extra_fields.
 */
function fb_social_comments_content_extra_fields($type_name) {
  if (fb_social_comments_type($type_name)) {
    $extras['fb_social_comments_widget'] = array(
      'label' => t('Facebook socials comments widget'),
      'description' => t('The facebook comments pluging widget'),
      'weight' => 20,
    );
    return $extras;
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function fb_social_comments_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'view':
      if ($node->status && fb_social_comments_type($node->type)) {
        if ($a4) {

          //only in page view
          $output = theme('fb_social_comments_block_comments_view', fb_social_url('node/' . $node->nid));
          $weight = module_exists('content') ? content_extra_field_weight($node->type, 'fb_social_comments_widget') : 20;
          $node->content['fb_social_comments_widget'] = array(
            '#weight' => $weight,
            '#value' => $output,
          );
        }
      }
      break;
  }
}

/**
 * theme function for the fb comments widget
 */
function theme_fb_social_comments_block_comments_view($url) {
  global $_fb_script_init;
  $_fb_script_init = TRUE;
  $xid = url($_GET['q'], array(
    'absolute' => TRUE,
    'alias' => TRUE,
  ));
  $attrs = array(
    'xid' => urlencode($xid),
    // why is drupal_urlencode not working ?
    'title' => $node->title,
    'numposts' => variable_get('fb_social_comments_numposts', 10),
    'width' => variable_get('fb_social_comments_width', 420),
    'colorscheme' => variable_get('fb_social_comments_colorscheme', 'light'),
  );

  // migrate to the new version of facebook comments
  $migrated = variable_get('fb_social_comments_migrated', 0);
  if ($migrated) {
    $attrs['migrated'] = '1';
  }
  else {
    $attrs['href'] = $url;
  }
  if ($css = variable_get('fb_social_comments_css', '')) {
    $attrs['css'] = $css;
  }
  $res = '<div id="fb-social-comments">';
  $res .= '<fb:comments ' . drupal_attributes($attrs) . '>';

  //$res .= '<fb:title>Comments on: ' .$node -> title. '</fb:title>';
  $res .= '</fb:comments>';
  $res .= '</div>';
  return $res;
}

/**
 * helper function for testing wether $type 
 * is selected to be a fb_social_comment type
 */
function fb_social_comments_type($type) {
  $fb_like_types = variable_get('fb_social_comments_node_types', array());
  return isset($fb_like_types[$type]) && $fb_like_types[$type] ? TRUE : FALSE;
}

Functions

Namesort descending Description
fb_social_comments_block Implementation of hook_block
fb_social_comments_content_extra_fields Implementation of hook_content_extra_fields.
fb_social_comments_menu Implementation of hook_menu
fb_social_comments_nodeapi Implementation of hook_nodeapi().
fb_social_comments_theme Implementation of hook_theme
fb_social_comments_type helper function for testing wether $type is selected to be a fb_social_comment type
theme_fb_social_comments_block_comments_view theme function for the fb comments widget