You are here

facebook_comments_block.module in Facebook Comments Block 7

Code for the Facebook Comments Block module.

File

facebook_comments_block.module
View source
<?php

/**
 * @file
 * Code for the Facebook Comments Block module.
 */

/**
 * Implements hook_block_info().
 */
function facebook_comments_block_block_info() {
  $blocks = array();
  $blocks['fb_comments'] = array(
    'info' => t('Facebook Comments'),
    'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

/**
 * Implements hook_block_info().
 */
function facebook_comments_block_help($path, $arg) {
  switch ($path) {
    case 'admin/help#facebook_comments_block':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('This module allows users to comment using Facebook\'s commenting box with as little configuration as possible. It adds a new block "Facebook Comments" which acts as a Facebook commenting widget.') . '</p>';
      $output .= '<h3>' . t('Configuration') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t("This block can be enabled and configured on the <a href='@admin-block'>Blocks administration page</a>.", array(
        '@admin-block' => url('admin/structure/block'),
      )) . '</dt>';
      $output .= '<dd>' . t("Find 'Facebook Comments' block and click on configure.") . '</a></dd>';
      $output .= '<dt>' . t('Under the "FACEBOOK COMMENTS BOX SETTINGS" you can configure the following.') . '</dt>';
      $output .= '<dd>' . t('Facebook Application ID: (Optional).') . '</dd>';
      $output .= '<dd>' . t('Main domain: (Optional) If you have more than one domain you can set the main domain for facebook comments box to use the same commenting widget across all other domains.') . '</dd>';
      $output .= '<dd>' . t('Language: Select the default language of the comments plugin.') . '</dd>';
      $output .= '<dd>' . t('Color Scheme: Set the color schema of facebook comments box.') . '</dd>';
      $output .= '<dd>' . t('Order of comments: Set the order of comments.') . '</dd>';
      $output .= '<dd>' . t('Number of posts: Select how many posts you want to display by default.') . '</dd>';
      $output .= '<dd>' . t('Width: Set width of facebook comments box.') . '</dd>';
      $output .= '</dl>';
      $output .= '<h3>' . t('Troubleshooting') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('If the block did not appear:') . '</dt>';
      $output .= '<dd>' . t("check if you have entered a correct FACEBOOK APP ID, leave it blank in case you don't have a Facebook app.") . '</a></dd>';
      $output .= '</dl>';
      return $output;
  }
}

/**
 * Implements hook_block_configure().
 */
function facebook_comments_block_block_configure($delta = '') {
  $form = array();
  switch ($delta) {
    case 'fb_comments':
      $form['facebook_comments_settings'] = array(
        '#type' => 'fieldset',
        '#title' => t('Facebook comments box settings'),
        '#description' => t('Configure facebook comments box.'),
        '#collapsible' => FALSE,
      );
      $form['facebook_comments_settings']['facebook_comments_block_settings_app_id'] = array(
        '#type' => 'textfield',
        '#title' => t('Facebook Application ID'),
        '#default_value' => variable_get('facebook_comments_block_settings_app_id', ''),
        '#maxlength' => 20,
        '#description' => t('Optional: Enter Facebook App ID.'),
      );
      $form['facebook_comments_settings']['facebook_comments_block_settings_domain'] = array(
        '#type' => 'textfield',
        '#title' => t('Main domain'),
        '#default_value' => variable_get('facebook_comments_block_settings_domain', ''),
        '#maxlength' => 75,
        '#description' => t('Optional: If you have more than one domain you can set the main domain for facebook comments box to use the same commenting widget across all other domains.<br />ex: <i>http://www.mysite.com</i>'),
        '#element_validate' => array(
          'facebook_comments_block_validate_absolute_url',
        ),
        '#required' => FALSE,
      );
      $form['facebook_comments_settings']['facebook_comments_block_settings_lang'] = array(
        '#type' => 'select',
        '#title' => t('Language'),
        '#options' => array(
          'af_ZA' => t('Afrikaans'),
          'ak_GH' => t('Akan'),
          'am_ET' => t('Amharic'),
          'ar_AR' => t('Arabic'),
          'as_IN' => t('Assamese'),
          'ay_BO' => t('Aymara'),
          'az_AZ' => t('Azerbaijani'),
          'be_BY' => t('Belarusian'),
          'bg_BG' => t('Bulgarian'),
          'bn_IN' => t('Bengali'),
          'br_FR' => t('Breton'),
          'bs_BA' => t('Bosnian'),
          'ca_ES' => t('Catalan'),
          'cb_IQ' => t('Sorani Kurdish'),
          'ck_US' => t('Cherokee'),
          'co_FR' => t('Corsican'),
          'cs_CZ' => t('Czech'),
          'cx_PH' => t('Cebuano'),
          'cy_GB' => t('Welsh'),
          'da_DK' => t('Danish'),
          'de_DE' => t('German'),
          'el_GR' => t('Greek'),
          'en_GB' => t('English - UK'),
          'en_IN' => t('English - India'),
          'en_PI' => t('English - Pirate'),
          'en_UD' => t('English - Upside Down'),
          'en_US' => t('English - US'),
          'eo_EO' => t('Esperanto'),
          'es_CL' => t('Spanish - Chile'),
          'es_CO' => t('Spanish - Colombia'),
          'es_ES' => t('Spanish - Spain'),
          'es_LA' => t('Spanish'),
          'es_MX' => t('Spanish - Mexico'),
          'es_VE' => t('Spanish - Venezuela'),
          'et_EE' => t('Estonian'),
          'eu_ES' => t('Basque'),
          'fa_IR' => t('Persian'),
          'fb_LT' => t('Leet Speak'),
          'ff_NG' => t('Fulah'),
          'fi_FI' => t('Finnish'),
          'fo_FO' => t('Faroese'),
          'fr_CA' => t('French - Canada'),
          'fr_FR' => t('French - France'),
          'fy_NL' => t('Frisian'),
          'ga_IE' => t('Irish'),
          'gl_ES' => t('Galician'),
          'gn_PY' => t('Guarani'),
          'gu_IN' => t('Gujarati'),
          'gx_GR' => t('Classical Greek'),
          'ha_NG' => t('Hausa'),
          'he_IL' => t('Hebrew'),
          'hi_IN' => t('Hindi'),
          'hr_HR' => t('Croatian'),
          'ht_HT' => t('Haitian Creole'),
          'hu_HU' => t('Hungarian'),
          'hy_AM' => t('Armenian'),
          'id_ID' => t('Indonesian'),
          'ig_NG' => t('Igbo'),
          'is_IS' => t('Icelandic'),
          'it_IT' => t('Italian'),
          'ja_JP' => t('Japanese'),
          'ja_KS' => t('Japanese - Kansai'),
          'jv_ID' => t('Javanese'),
          'ka_GE' => t('Georgian'),
          'kk_KZ' => t('Kazakh'),
          'km_KH' => t('Khmer'),
          'kn_IN' => t('Kannada'),
          'ko_KR' => t('Korean'),
          'ku_TR' => t('Kurdish - Kurmanji'),
          'ky_KG' => t('Kyrgyz'),
          'la_VA' => t('Latin'),
          'lg_UG' => t('Ganda'),
          'li_NL' => t('Limburgish'),
          'ln_CD' => t('Lingala'),
          'lo_LA' => t('Lao'),
          'lt_LT' => t('Lithuanian'),
          'lv_LV' => t('Latvian'),
          'mg_MG' => t('Malagasy'),
          'mi_NZ' => t('Māori'),
          'mk_MK' => t('Macedonian'),
          'ml_IN' => t('Malayalam'),
          'mn_MN' => t('Mongolian'),
          'mr_IN' => t('Marathi'),
          'ms_MY' => t('Malay'),
          'mt_MT' => t('Maltese'),
          'my_MM' => t('Burmese'),
          'nb_NO' => t('Norwegian - bokmal'),
          'nd_ZW' => t('Ndebele'),
          'ne_NP' => t('Nepali'),
          'nl_BE' => t('Dutch - België'),
          'nl_NL' => t('Dutch'),
          'nn_NO' => t('Norwegian - nynorsk'),
          'ny_MW' => t('Chewa'),
          'or_IN' => t('Oriya'),
          'pa_IN' => t('Punjabi'),
          'pl_PL' => t('Polish'),
          'ps_AF' => t('Pashto'),
          'pt_BR' => t('Portuguese - Brazil'),
          'pt_PT' => t('Portuguese - Portugal'),
          'qc_GT' => t('Quiché'),
          'qu_PE' => t('Quechua'),
          'rm_CH' => t('Romansh'),
          'ro_RO' => t('Romanian'),
          'ru_RU' => t('Russian'),
          'rw_RW' => t('Kinyarwanda'),
          'sa_IN' => t('Sanskrit'),
          'sc_IT' => t('Sardinian'),
          'se_NO' => t('Northern Sámi'),
          'si_LK' => t('Sinhala'),
          'sk_SK' => t('Slovak'),
          'sl_SI' => t('Slovenian'),
          'sn_ZW' => t('Shona'),
          'so_SO' => t('Somali'),
          'sq_AL' => t('Albanian'),
          'sr_RS' => t('Serbian'),
          'sv_SE' => t('Swedish'),
          'sw_KE' => t('Swahili'),
          'sy_SY' => t('Syriac'),
          'sz_PL' => t('Silesian'),
          'ta_IN' => t('Tamil'),
          'te_IN' => t('Telugu'),
          'tg_TJ' => t('Tajik'),
          'th_TH' => t('Thai'),
          'tk_TM' => t('Turkmen'),
          'tl_PH' => t('Filipino'),
          'tl_ST' => t('Klingon'),
          'tr_TR' => t('Turkish'),
          'tt_RU' => t('Tatar'),
          'tz_MA' => t('Tamazight'),
          'uk_UA' => t('Ukrainian'),
          'ur_PK' => t('Urdu'),
          'uz_UZ' => t('Uzbek'),
          'vi_VN' => t('Vietnamese'),
          'wo_SN' => t('Wolof'),
          'xh_ZA' => t('Xhosa'),
          'yi_DE' => t('Yiddish'),
          'yo_NG' => t('Yoruba'),
          'zh_CN' => t('Simplified Chinese - China'),
          'zh_HK' => t('Traditional Chinese - Hong Kong'),
          'zh_TW' => t('Traditional Chinese - Taiwan'),
          'zu_ZA' => t('Zulu'),
          'zz_TR' => t('Zazaki'),
        ),
        '#default_value' => variable_get('facebook_comments_block_settings_lang', 'en_US'),
        '#description' => t('Select the default language of the comments plugin.'),
        '#required' => TRUE,
      );
      $form['facebook_comments_settings']['facebook_comments_block_settings_color_schema'] = array(
        '#type' => 'select',
        '#title' => t('Color scheme'),
        '#options' => array(
          'light' => t('Light'),
          'dark' => t('Dark'),
        ),
        '#default_value' => variable_get('facebook_comments_block_settings_color_schema', 'light'),
        '#description' => t('Set the color schema of facebook comments box.'),
        '#required' => TRUE,
      );
      $form['facebook_comments_settings']['facebook_comments_block_settings_order'] = array(
        '#type' => 'select',
        '#title' => t('Order of comments'),
        '#options' => array(
          'social' => t('Top'),
          'reverse_time' => t('Newest'),
          'time' => t('Oldest'),
        ),
        '#default_value' => variable_get('facebook_comments_block_settings_order', 'social'),
        '#description' => t('Set the order of comments.'),
        '#required' => TRUE,
      );
      $form['facebook_comments_settings']['facebook_comments_block_settings_number_of_posts'] = array(
        '#type' => 'textfield',
        '#title' => t('Number of posts'),
        '#default_value' => variable_get('facebook_comments_block_settings_number_of_posts', '5'),
        '#maxlength' => 3,
        '#description' => t('Select how many posts you want to display by default.'),
        '#required' => TRUE,
      );
      $form['facebook_comments_settings']['facebook_comments_block_settings_width'] = array(
        '#type' => 'textfield',
        '#title' => t('Width'),
        '#default_value' => variable_get('facebook_comments_block_settings_width', '500'),
        '#maxlength' => 4,
        '#description' => t('Set width of facebook comments box.'),
        '#required' => TRUE,
      );
      break;
  }
  return $form;
}

/**
 * Helper function to validate absolute URL.
 */
function facebook_comments_block_validate_absolute_url($element, &$form_state) {

  // trim trailing slash from the URL.
  $value = rtrim(rtrim($element['#value']), '/');
  if ($value !== '' && !valid_url($value, TRUE)) {
    form_error($element, t('%name must be a valid absolute URL.', array(
      '%name' => $element['#title'],
    )));
  }
}

/**
 * Implements hook_block_save().
 */
function facebook_comments_block_block_save($delta = '', $edit = array()) {
  switch ($delta) {
    case 'fb_comments':
      variable_set('facebook_comments_block_settings_app_id', $edit['facebook_comments_block_settings_app_id']);
      variable_set('facebook_comments_block_settings_domain', rtrim(rtrim($edit['facebook_comments_block_settings_domain']), '/'));
      variable_set('facebook_comments_block_settings_lang', $edit['facebook_comments_block_settings_lang']);
      variable_set('facebook_comments_block_settings_color_schema', $edit['facebook_comments_block_settings_color_schema']);
      variable_set('facebook_comments_block_settings_order', $edit['facebook_comments_block_settings_order']);
      variable_set('facebook_comments_block_settings_width', $edit['facebook_comments_block_settings_width']);
      variable_set('facebook_comments_block_settings_number_of_posts', $edit['facebook_comments_block_settings_number_of_posts']);
      break;
  }
}

/**
 * Implements hook_theme().
 */
function facebook_comments_block_theme() {
  return array(
    'facebook_comments__block' => array(
      'template' => 'templates/facebook-comments--block',
      'variables' => array(
        'facebook' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_block_view().
 */
function facebook_comments_block_block_view($delta = '') {
  global $base_url;
  $main_domain = $base_url;
  if (variable_get('facebook_comments_block_settings_domain', '') !== '') {
    $main_domain = variable_get('facebook_comments_block_settings_domain', '');
  }
  $block = array();
  switch ($delta) {
    case 'fb_comments':
      $url = $main_domain . '/' . current_path();
      $facebook_app_id = variable_get('facebook_comments_block_settings_app_id', '');
      $facebook_app_id_script = $facebook_app_id != '' ? "&appId={$facebook_app_id}" : '';
      $facebook_app_lang = variable_get('facebook_comments_block_settings_lang', 'en_US');
      $js_vars = array(
        'facebook_app_id' => $facebook_app_id,
        'facebook_app_id_script' => $facebook_app_id_script,
        'facebook_app_lang' => $facebook_app_lang,
      );
      $theme_vars = array(
        'facebook' => array(
          'data_attributes' => array(
            'href' => $url,
            'data-href' => $url,
            'data-width' => variable_get('facebook_comments_block_settings_width', '500'),
            'data-numposts' => variable_get('facebook_comments_block_settings_number_of_posts', '5'),
            'data-colorscheme' => variable_get('facebook_comments_block_settings_color_schema', 'light'),
            'data-order-by' => variable_get('facebook_comments_block_settings_order', 'top'),
          ),
        ),
      );
      $facebook_app_id_meta = array(
        '#type' => 'html_tag',
        '#tag' => 'meta',
        '#attributes' => array(
          'property' => 'fb:app_id',
          'content' => $facebook_app_id,
        ),
      );
      drupal_add_html_head($facebook_app_id_meta, 'facebook_app_id_meta');
      $block = array(
        'subject' => t('Facebook comments'),
        'content' => theme('facebook_comments__block', $theme_vars),
      );
      drupal_add_js(array(
        'facebook_comments_block' => array(
          'facebook_settings' => $js_vars,
        ),
      ), array(
        'type' => 'setting',
      ));
      drupal_add_js(drupal_get_path('module', 'facebook_comments_block') . '/js/facebook_comments_block.js', array(
        'scope' => 'footer',
        'type' => 'file',
      ));
      break;
  }
  return $block;
}