facebook_comments.module in Facebook Comments Social Plugin 7
Same filename and directory in other branches
Facebook Comments Social Plugin module file.
File
facebook_comments.moduleView source
<?php
/**
* @file
* Facebook Comments Social Plugin module file.
*/
/**
* Implements of hook_permission().
*/
function facebook_comments_permission() {
return array(
'administer facebook comments' => array(
'title' => t('Administer Facebook comments'),
),
'moderate facebook comments' => array(
'title' => t('Enable/disable Facebook comments per node'),
),
);
}
/**
* Implements of hook_menu().
*/
function facebook_comments_menu() {
$items = array();
$items['admin/config/content/facebook-comments'] = array(
'title' => 'Facebook comments settings',
'description' => 'Configure Facebook comments settings like the Facebook App ID.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'facebook_comments_admin',
),
'access arguments' => array(
'administer facebook comments',
),
);
return $items;
}
/**
* Implements hook_block_info().
*/
function facebook_comments_block_info() {
$blocks = array();
$blocks['facebook-comments'] = array(
'info' => t('Facebook comments'),
'cache' => DRUPAL_CACHE_PER_PAGE,
);
return $blocks;
}
/**
* Implements hook_block_configure().
*/
function facebook_comments_block_configure($delta = '') {
$form = array();
if ($delta == 'facebook-comments') {
$form['facebook_comments_block_style'] = array(
'#type' => 'select',
'#title' => t('Color Scheme'),
'#default_value' => variable_get('facebook_comments_block_style', 'light'),
'#options' => array(
'light' => t('Light'),
'dark' => t('Dark'),
),
);
$form['facebook_comments_block_width'] = array(
'#type' => 'textfield',
'#title' => t('Facebook comment plugin width'),
'#default_value' => variable_get('facebook_comments_block_width', 208),
'#description' => t('The width of the Facebook comment plugin for the block, in pixels. Example: 208'),
);
$form['facebook_comments_block_width_fluid'] = array(
'#type' => 'checkbox',
'#title' => t('Fluid Facebook comment plugin width'),
'#default_value' => variable_get('facebook_comments_block_width_fluid', 1),
'#description' => t('Make the width of the Facebook comment plugin for the block fluid (100%). This overrules the width settings above.'),
);
$form['facebook_comments_block_amount'] = array(
'#type' => 'select',
'#title' => t('Amount of comments to display'),
'#options' => array(
1 => 1,
2 => 2,
3 => 3,
5 => 5,
7 => 7,
10 => 10,
15 => 15,
20 => 20,
30 => 30,
),
'#default_value' => variable_get('facebook_comments_block_amount', 5),
);
}
return $form;
}
/**
* Implements hook_block_save().
*/
function facebook_comments_block_save($delta = '', $edit = array()) {
if ($delta == 'facebook-comments') {
variable_set('facebook_comments_block_style', $edit['facebook_comments_block_style']);
variable_set('facebook_comments_block_width', $edit['facebook_comments_block_width']);
variable_set('facebook_comments_block_width_fluid', $edit['facebook_comments_block_width_fluid']);
variable_set('facebook_comments_block_amount', $edit['facebook_comments_block_amount']);
}
}
/**
* Implements hook_block_view().
*/
function facebook_comments_block_view($delta = '') {
$block = array();
if ($delta == 'facebook-comments') {
$width = variable_get('facebook_comments_block_width', 208);
$fluid = variable_get('facebook_comments_block_width_fluid', 1);
$amount = variable_get('facebook_comments_block_amount', 5);
$block = array(
'subject' => t('Facebook comments'),
'content' => facebook_comments_display('block', $width, $amount, $fluid),
);
}
return $block;
}
/**
* Configure Facebook comments settings like the Facebook App ID.
*
* @see facebook_comments_admin_applyall()
*/
function facebook_comments_admin() {
$form = array();
$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_viewmode'] = array(
'#type' => 'select',
'#title' => t('View mode'),
'#default_value' => variable_get('facebook_comments_viewmode', 'full'),
'#options' => array(
'both' => t('Both full node and teaser'),
'full' => t('Full node'),
'teaser' => t('Teaser'),
),
);
$form['facebook_comments_views'] = array(
'#type' => 'checkbox',
'#title' => t('Views support'),
'#default_value' => variable_get('facebook_comments_views', 0),
'#description' => t('Enable support for the Views module. Warning: you might lose comments on existing pages.'),
);
$form['facebook_comments_ssl'] = array(
'#type' => 'checkbox',
'#title' => t('SSL support'),
'#default_value' => variable_get('facebook_comments_ssl', 0),
'#description' => t('Enable support for SSL. Warning: you might lose comments on existing pages.'),
);
$form['facebook_comments_status'] = array(
'#type' => 'checkbox',
'#title' => t('Show comments on unpublished nodes'),
'#default_value' => variable_get('facebook_comments_status', 0),
);
$form['facebook_comments_width'] = array(
'#type' => 'textfield',
'#title' => t('Facebook comment plugin width (nodes)'),
'#default_value' => variable_get('facebook_comments_width', 620),
'#description' => t('The width of the Facebook comment plugin for nodes, in pixels. Example: 620'),
);
$form['facebook_comments_width_fluid'] = array(
'#type' => 'checkbox',
'#title' => t('Fluid Facebook comment plugin width'),
'#default_value' => variable_get('facebook_comments_width_fluid', 1),
'#description' => t('Make the width of the Facebook comment plugin fluid (100%). This overrules the width settings above.'),
);
$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_admins'] = array(
'#type' => 'textfield',
'#title' => t('Facebook Admins'),
'#default_value' => variable_get('facebook_comments_admins', ''),
'#description' => t('Enter a comma-seperated list of all Facebook admin user id\'s to ensure that all comments can be grouped for moderation.<br/>If you enter an App ID, the Admins will be ignored. For more information read the !url.', array(
'!url' => l(t('developer documentation'), 'https://developers.facebook.com/docs/plugins/comments#moderation-setup-instructions', array(
'absolute' => TRUE,
)),
)),
);
$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.<br/>This default value can be changed per node by users with the "Enable/disable Facebook comments per node" permission.'),
);
$form['facebook_comments_applyall'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Facebook comments on existing content for the selected content types above.'),
'#default_value' => FALSE,
);
$form['#submit'][] = 'facebook_comments_admin_applyall';
return system_settings_form($form);
}
/**
* Form submission handler for facebook_comments_admin().
*
* @see facebook_comments_admin()
*/
function facebook_comments_admin_applyall(&$form, $form_state) {
if ($form_state['values']['facebook_comments_applyall']) {
$types = array();
foreach ($form_state['values']['facebook_comments_types'] as $key => $value) {
if (!empty($value)) {
$types[] = $key;
}
}
$results = db_select('node', 'n')
->fields('n', array(
'nid',
))
->condition('type', $types, 'IN')
->execute();
while ($result = $results
->fetchAssoc()) {
db_merge('facebook_comments')
->key(array(
'nid' => $result['nid'],
))
->fields(array(
'enabled' => 1,
))
->execute();
}
drupal_set_message(t('Facebook comments have been enabled on existing content for the selected content types.'));
}
}
/**
* Implements hook_field_extra_fields().
*/
function facebook_comments_field_extra_fields() {
$extra = array();
$default_types = variable_get('facebook_comments_types', array());
foreach (node_type_get_types() as $type) {
if (facebook_comments_is_enabled($type->type)) {
$extra['node'][$type->type] = array(
'display' => array(
'facebook_comments' => array(
'label' => t('Facebook comments'),
'description' => t('Facebook comments'),
'weight' => 1002,
),
),
);
}
}
return $extra;
}
/**
* Helper function to determine if Facebook commenting is enabled.
*/
function facebook_comments_is_enabled($type) {
$default_types = variable_get('facebook_comments_types', array());
if (isset($default_types[$type]) && $default_types[$type] === $type) {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_form_alter().
*
* Add the Facebook commenting options for a node.
*/
function facebook_comments_form_node_form_alter(&$form, $form_state) {
// Load the default values.
$node = $form['#node'];
// If this is a preview then get the values from the form, not the db.
if (isset($form_state['values']['op']) && $form_state['values']['op'] == t('Preview')) {
$defaults = new StdClass();
$defaults->enabled = $form_state['values']['facebook_comments_enabled'];
$defaults->amount = $form_state['values']['facebook_comments_amount'];
}
elseif (isset($node->nid) && $node->nid > 0) {
// Load the values from the db if we are viewing an existing node.
$defaults = db_select('facebook_comments', 'f')
->fields('f', array(
'enabled',
'amount',
))
->condition('f.nid', $node->nid, '=')
->execute()
->fetchObject();
// If the node existed before we installed facebook_comments add, default values.
if (!$defaults) {
$defaulttypes = variable_get('facebook_comments_types', array());
$defaults = new StdClass();
$defaults->enabled = FALSE;
$defaults->amount = 15;
}
}
else {
// Init standard values.
$defaulttypes = variable_get('facebook_comments_types', array());
$defaults = new StdClass();
$defaults->enabled = !empty($defaulttypes[$node->type]) ? 1 : 0;
$defaults->amount = 15;
}
$form['facebook_comments'] = array(
'#type' => 'fieldset',
'#title' => t('Facebook comments'),
'#group' => 'additional_settings',
'#access' => user_access('moderate facebook comments'),
'#attributes' => array(
'class' => array(
'edit-facebook-comments',
),
),
'#attached' => array(
'js' => array(
'vertical-tabs' => drupal_get_path('module', 'facebook_comments') . "/facebook_comments_vertical_tabs.js",
),
),
);
// Enable or disable Facebook comments for this node.
$form['facebook_comments']['facebook_comments_enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Facebook comments'),
'#default_value' => $defaults->enabled,
);
// Amount of comments.
$form['facebook_comments']['facebook_comments_amount'] = array(
'#type' => 'select',
'#title' => t('Amount of comments to display'),
'#options' => array(
1 => 1,
2 => 2,
3 => 3,
5 => 5,
7 => 7,
10 => 10,
15 => 15,
20 => 20,
30 => 30,
),
'#default_value' => $defaults->amount,
);
}
/**
* Implements hook_node_insert().
*/
function facebook_comments_node_insert($node) {
if (isset($node->facebook_comments_enabled) && !empty($node->facebook_comments_enabled)) {
db_insert('facebook_comments')
->fields(array(
'nid' => $node->nid,
'enabled' => $node->facebook_comments_enabled,
'amount' => $node->facebook_comments_amount,
))
->execute();
}
}
/**
* Implements hook_node_update().
*/
function facebook_comments_node_update($node) {
if (isset($node->facebook_comments_enabled)) {
db_merge('facebook_comments')
->key(array(
'nid' => $node->nid,
))
->fields(array(
'enabled' => $node->facebook_comments_enabled,
'amount' => $node->facebook_comments_amount,
))
->execute();
}
}
/**
* Implements hook_node_delete().
*/
function facebook_comments_node_delete($node) {
db_delete('facebook_comments')
->condition('nid', $node->nid)
->execute();
}
/**
* Implements hook_node_view().
*/
function facebook_comments_node_view($node, $view_mode, $langcode) {
// Check if the node is published.
if (!variable_get('facebook_comments_status', 0) && !$node->status) {
return;
}
// Check the view mode to display the comments or not.
$fc_viewmode = variable_get('facebook_comments_viewmode', 'full');
if ($fc_viewmode != "both" && $view_mode != $fc_viewmode) {
return;
}
// Check if Facebook comments are enabled for this node.
$comments = db_select('facebook_comments', 'f')
->fields('f', array(
'enabled',
'amount',
))
->condition('f.nid', $node->nid, '=')
->execute()
->fetchObject();
if (!isset($comments->enabled) || !$comments->enabled) {
return;
}
// Add the Facebook App ID if it exists.
$width = variable_get('facebook_comments_width', 620);
$fluid = variable_get('facebook_comments_width_fluid', 1);
$fragment = variable_get('facebook_comments_views', 0) ? $node->nid : 0;
$output = facebook_comments_display('node', $width, $comments->amount, $fluid, $fragment);
$node->content['facebook_comments'] = array(
'#markup' => $output,
'#weight' => 1002,
);
}
/**
* Generate the output of a Facebook commenting plugin.
*
* @param int $width
* The width of the plugin in pixels.
* @param int $amount
* The amount of comments to display.
* @param int $fluid
* Use fluid width.
* @param int $fragment
* Add the nid as a fragment.
*/
function facebook_comments_display($mode = 'node', $width, $amount, $fluid = 1, $fragment = 0) {
// 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');
}
elseif ($admins = variable_get('facebook_comments_admins', '')) {
$admin = explode(",", $admins);
foreach ($admin as $key => $value) {
$element = array(
'#tag' => 'meta',
'#attributes' => array(
'property' => 'fb:admins',
'content' => $value,
),
);
drupal_add_html_head($element, 'facebook_comments_' . $key);
}
}
if ($fluid) {
$class = "fb-comments-fluid";
drupal_add_css('#fbcomments, .fb-comments, .fb-comments iframe, .fb-comments span {width: 100% !important;}', array(
'type' => 'inline',
));
}
else {
$class = "";
}
// Generate the URL.
$options = array(
'absolute' => TRUE,
);
if ($fragment) {
$options['fragment'] = $fragment;
}
$url = url(current_path(), $options);
// If the path is non-SSL, rewrite it to SSL.
if (variable_get('facebook_comments_ssl', 0) && strpos($url, "http://") !== FALSE) {
$url = str_ireplace("http://", "https://", $url);
}
// Add user defined settings.
if ($mode == 'block') {
$style = variable_get('facebook_comments_block_style', 'light');
}
else {
$style = variable_get('facebook_comments_style', 'light');
}
$lang = _facebook_comments_get_language_code();
$output = theme('facebook_comments_display_' . $mode, array(
'class' => $class,
'url' => $url,
'amount' => $amount,
'width' => $width,
'style' => $style,
'lang' => $lang,
));
return $output;
}
function facebook_comments_theme() {
return array(
'facebook_comments_display_node' => array(
'path' => drupal_get_path('module', 'facebook_comments') . "/templates",
'template' => 'facebook-comments-display-node',
'variables' => array(
'class' => '',
'url' => '',
'amount' => 15,
'width' => 620,
'style' => 'light',
'lang' => 'en_GB',
),
),
'facebook_comments_display_block' => array(
'path' => drupal_get_path('module', 'facebook_comments') . "/templates",
'template' => 'facebook-comments-display-block',
'variables' => array(
'class' => '',
'url' => '',
'amount' => 5,
'width' => 208,
'style' => 'light',
'lang' => 'en_GB',
),
),
);
}
/**
* Returns a Language code format pt_PT. If the current language code is not in the conversion array it returns English as default language.
*/
function _facebook_comments_get_language_code() {
// Get global language.
global $language;
// Current language.
$current_language = $language->language;
// Get available languages.
$language_codes = _facebook_comments_get_language_codes();
$languages = array();
// Get available language codes.
if (array_key_exists($current_language, $language_codes)) {
$languages = $language_codes[$current_language];
}
else {
$languages = $language_codes['en'];
}
// Get the first elements key, for the available language codes.
reset($languages);
return key($languages);
}
/**
* Returns an array with the relation of the languages with its pt_PT code.
*/
function _facebook_comments_get_language_codes() {
return array(
'af' => array(
'af_ZA' => 'Afrikaans',
),
'ar' => array(
'ar_AR' => 'Arabic',
),
'ay' => array(
'ay_BO' => 'Aymara',
),
'az' => array(
'az_AZ' => 'Azeri',
),
'be' => array(
'be_BY' => 'Belarusian',
),
'bg' => array(
'bg_BG' => 'Bulgarian',
),
'bn' => array(
'bn_IN' => 'Bengali',
),
'bs' => array(
'bs_BA' => 'Bosnian',
),
'ca' => array(
'ca_ES' => 'Catalan',
),
'ck' => array(
'ck_US' => 'Cherokee',
),
'cs' => array(
'cs_CZ' => 'Czech',
),
'cy' => array(
'cy_GB' => 'Welsh',
),
'da' => array(
'da_DK' => 'Danish',
),
'de' => array(
'de_DE' => 'German',
),
'el' => array(
'el_GR' => 'Greek',
),
'en' => array(
'en_GB' => 'English (UK)',
'en_PI' => 'English (Pirate)',
'en_UD' => 'English (Upside Down)',
'en_US' => 'English (US)',
),
'eo' => array(
'eo_EO' => 'Esperanto',
),
'es' => array(
'es_LA' => 'Spanish',
'es_CL' => 'Spanish (Chile)',
'es_CO' => 'Spanish (Colombia)',
'es_MX' => 'Spanish (Mexico)',
'es_VE' => 'Spanish (Venezuela)',
),
'et' => array(
'et_EE' => 'Estonian',
),
'eu' => array(
'eu_ES' => 'Basque',
),
'fa' => array(
'fa_IR' => 'Persian',
),
'fb' => array(
'fb_FI' => 'Finnish (test)',
'fb_LT' => 'Leet Speak',
),
'fi' => array(
'fi_FI' => 'Finnish',
),
'fo' => array(
'fo_FO' => 'Faroese',
),
'fr' => array(
'fr_FR' => 'French (France)',
'fr_CA' => 'French (Canada)',
),
'ga' => array(
'ga_IE' => 'Irish',
),
'gl' => array(
'gl_ES' => 'Galician',
),
'gn' => array(
'gn_PY' => 'Guarani',
),
'gu' => array(
'gu_IN' => 'Gujarati',
),
'he' => array(
'he_IL' => 'Hebrew',
),
'hi' => array(
'hi_IN' => 'Hindi',
),
'hr' => array(
'hr_HR' => 'Croatian',
),
'hu' => array(
'hu_HU' => 'Hungarian',
),
'hy' => array(
'hy_AM' => 'Armenian',
),
'id' => array(
'id_ID' => 'Indonesian',
),
'is' => array(
'is_IS' => 'Icelandic',
),
'it' => array(
'it_IT' => 'Italian',
),
'ja' => array(
'ja_JP' => 'Japanese',
),
'jv' => array(
'jv_ID' => 'Javanese',
),
'ka' => array(
'ka_GE' => 'Georgian',
),
'kk' => array(
'kk_KZ' => 'Kazakh',
),
'km' => array(
'km_KH' => 'Khmer',
),
'kn' => array(
'kn_IN' => 'Kannada',
),
'ko' => array(
'ko_KR' => 'Korean',
),
'ku' => array(
'ku_TR' => 'Kurdish',
),
'la' => array(
'la_VA' => 'Latin',
),
'li' => array(
'li_NL' => 'Limburgish',
),
'lt' => array(
'lt_LT' => 'Lithuanian',
),
'lv' => array(
'lv_LV' => 'Latvian',
),
'mg' => array(
'mg_MG' => 'Malagasy',
),
'mk' => array(
'mk_MK' => 'Macedonian',
),
'ml' => array(
'ml_IN' => 'Malayalam',
),
'mn' => array(
'mn_MN' => 'Mongolian',
),
'mr' => array(
'mr_IN' => 'Marathi',
),
'ms' => array(
'ms_MY' => 'Malay',
),
'mt' => array(
'mt_MT' => 'Maltese',
),
'nb' => array(
'nb_NO' => 'Norwegian (bokmal)',
),
'ne' => array(
'ne_NP' => 'Nepali',
),
'nl' => array(
'nl_NL' => 'Dutch',
'nl_BE' => 'Dutch (Belgium)',
),
'nn' => array(
'nn_NO' => 'Norwegian (nynorsk)',
),
'pa' => array(
'pa_IN' => 'Punjabi',
),
'pl' => array(
'pl_PL' => 'Polish',
),
'ps' => array(
'ps_AF' => 'Pashto',
),
'pt' => array(
'pt_BR' => 'Portuguese (Brazil)',
),
'pt' => array(
'pt_PT' => 'Portuguese (Portugal)',
),
'qu' => array(
'qu_PE' => 'Quechua',
),
'rm' => array(
'rm_CH' => 'Romansh',
),
'ro' => array(
'ro_RO' => 'Romanian',
),
'ru' => array(
'ru_RU' => 'Russian',
),
'sa' => array(
'sa_IN' => 'Sanskrit',
),
'se' => array(
'se_NO' => 'Northern Sami',
),
'sk' => array(
'sk_SK' => 'Slovak',
),
'sl' => array(
'sl_SI' => 'Slovenian',
),
'so' => array(
'so_SO' => 'Somali',
),
'sq' => array(
'sq_AL' => 'Albanian',
),
'sr' => array(
'sr_RS' => 'Serbian',
),
'sv' => array(
'sv_SE' => 'Swedish',
),
'sw' => array(
'sw_KE' => 'Swahili',
),
'sy' => array(
'sy_SY' => 'Syriac',
),
'ta' => array(
'ta_IN' => 'Tamil',
),
'te' => array(
'te_IN' => 'Telugu',
),
'tg' => array(
'tg_TJ' => 'Tajik',
),
'th' => array(
'th_TH' => 'Thai',
),
'tl' => array(
'tl_PH' => 'Filipino',
'tl_ST' => 'Klingon',
),
'tr' => array(
'tr_TR' => 'Turkish',
),
'tt' => array(
'tt_RU' => 'Tatar',
),
'uk' => array(
'uk_UA' => 'Ukrainian',
),
'ur' => array(
'ur_PK' => 'Urdu',
),
'uz' => array(
'uz_UZ' => 'Uzbek',
),
'vi' => array(
'vi_VN' => 'Vietnamese',
),
'xh' => array(
'xh_ZA' => 'Xhosa',
),
'yi' => array(
'yi_DE' => 'Yiddish',
),
'zh' => array(
'zh_CN' => 'Simplified Chinese (China)',
'zh_HK' => 'Traditional Chinese (Hong Kong)',
'zh_TW' => 'Traditional Chinese (Taiwan)',
),
'zu' => array(
'zu_ZA' => 'Zulu',
),
);
}