You are here

fblikebutton.module in Facebook Like Button 6

Adds Facebook's "Like" button to each selected node type. Adds a block with a global static value where users can "Like" the URL set by admins.

File

fblikebutton.module
View source
<?php

// $Id$

/**
 * @file
 * Adds Facebook's "Like" button to each selected node type.
 * Adds a block with a global static value where users can "Like" the URL set by admins.
 */

/**
 * Implementation of hook_menu().
 */
function fblikebutton_menu() {
  $items['admin/settings/fblikebutton'] = array(
    'title' => 'FB Like Button',
    'description' => 'Configure Facebook <em>Like</em> button settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'fblikebutton_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'fblikebutton.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_nodeapi().
 */
function fblikebutton_nodeapi(&$node, $op, $teaser, $page) {
  global $user, $base_url, $language;
  $likebase = $base_url . '/';
  $likepath = drupal_get_path_alias('node/' . $node->nid);
  $webpage_to_like = $likebase . $likepath;
  $lang_abbr = $language->language;
  $fblikebutton_lang = $lang_abbr . '_' . drupal_strtoupper($lang_abbr);
  switch ($op) {
    case 'view':

      // Whether to show faces in the iframe. (Thanks to dizarter for pointing out that the next few lines should be relocated to where they are now.)
      $fblikebutton_showonteasers = variable_get('fblikebutton_showonteasers', 0);

      // Set which node types users can "like".
      $types_to_like = variable_get('fblikebutton_node_types', array(
        'page',
      ));

      // Keep the fblikebutton button out of search results, teasers, etc., if set.
      if ($fblikebutton_showonteasers == 0) {
        if (!$page) {
          break;
        }
      }

      // Do not add the like button to any of the unchecked node types.
      if (!in_array($node->type, $types_to_like, TRUE)) {
        break;
      }

      // Whether or not to show the faces of people who have "liked" the node.
      $show_faces = variable_get('fblikebutton_show_faces', 'true');

      // Color scheme of the box. Options: "light" or "dark".
      $colorscheme = variable_get('fblikebutton_color_scheme', 'light');

      // Layout style. Options: "Standard", "Box Count", or "Button Count".
      $layout = variable_get('fblikebutton_layout', 'standard');

      // Action to display. Options: "Like" or "Recommend"
      $action = variable_get('fblikebutton_action', 'like');

      // Font to use.
      $font = variable_get('fblikebutton_font', 'arial');

      // Iframe height.
      $iframe_h = variable_get('fblikebutton_iframe_height', '80');

      // Iframe width.
      $iframe_w = variable_get('fblikebutton_iframe_width', '450');

      // Extra CSS to be tacked on to the end of the iframe. Example: padding-left: 5px; padding-top: 10px;
      $other_css = variable_get('fblikebutton_iframe_css', '');
      $other_css = trim($other_css);

      // Weight on the node
      $likebutton_weight = variable_get('fblikebutton_weight', '50');
      $likebutton_lang = variable_get('fblikebutton_language', $fblikebutton_lang);

      // The next part is whether or not to display the "Send" button next to the "Like" (or "Recommend") button.
      // CURRENTLY ONLY WORKS WITH XFBML (NOT IFRAME)... But it doesn't hurt anything and WILL
      // be in one of the next stable releases, so I'm leaving it there in case FB decides to enable
      // the feature to work with the iframe version. Until then, or until next stable release, see dev snapshot
      // if you require the "Send" button.
      $displaysend = variable_get('fblikebutton_send', 'true');

      // Since we're currently using the iframe version...
      $likebutton = '<iframe class="fblikebutton" src="https://www.facebook.com/plugins/like.php?href=' . urlencode($webpage_to_like) . '&amp;send=' . check_plain($displaysend) . '&amp;layout=' . check_plain($layout) . '&amp;show_faces=' . check_plain($show_faces) . '&amp;width=' . check_plain($iframe_w) . '&amp;action=' . check_plain($action) . '&amp;font=' . check_plain($font) . '&amp;colorscheme=' . check_plain($colorscheme) . '&amp;height=' . check_plain($iframe_h) . '&amp;locale=' . check_plain($likebutton_lang) . '" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:' . check_plain($iframe_w) . 'px; height:' . check_plain($iframe_h) . 'px;' . check_plain($other_css) . '" allowTransparency="true"></iframe>';

      // Check permissions
      if (user_access('users may access Like button')) {

        // If set, keep the button out of teasers:
        if ($fblikebutton_showonteasers == 0) {
          if ($teaser) {
            break;
          }
        }
        $node->content['fblikebutton_button'] = array(
          '#value' => $likebutton,
          '#weight' => $likebutton_weight,
        );
        break;
      }
  }
}

/**
 * Implementation of hook_perm().
 */
function fblikebutton_perm() {
  return array(
    'users may administer Like button',
    'users may administer Like button block',
    'users may access Like button',
  );
}

/**
 * Implementation of hook_block().
 */
function fblikebutton_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('FB Like Button');
      $blocks[0]['cache'] = BLOCK_NO_CACHE;
      return $blocks;
    case 'configure':
      global $base_url, $language;
      $block_lang_abbr = $language->language;
      $fblikebutton_block_lang = $block_lang_abbr . '_' . drupal_strtoupper($block_lang_abbr);
      if ($delta == 0 && user_access('users may administer Like button block')) {
        $form['fblikebutton_block_url'] = array(
          '#title' => t('URL to display'),
          '#type' => 'textfield',
          '#default_value' => variable_get('fblikebutton_block_url', $base_url),
          '#description' => t('URL of the webpage to like (default is the base URL of this site: @site). This value will remain the same throughout your site.', array(
            '@site' => $base_url,
          )),
        );
        $form['fblikebutton_block'] = array(
          '#type' => 'fieldset',
          '#title' => 'Block configuration',
          '#collapsible' => false,
        );
        $form['fblikebutton_block']['fblikebutton_bl_layout'] = array(
          '#type' => 'select',
          '#title' => t('Layout style'),
          '#options' => array(
            'standard' => t('Standard'),
            'box_count' => t('Box Count'),
            'button_count' => t('Button Count'),
          ),
          '#default_value' => variable_get('fblikebutton_bl_layout', 'standard'),
          '#description' => t('Determines the size and amount of social context next to the button.'),
        );
        $form['fblikebutton_block']['fblikebutton_bl_show_faces'] = array(
          '#type' => 'select',
          '#title' => t('Display faces in the box'),
          '#options' => array(
            'showz' => t('Show faces'),
            'hide' => t('Do not show faces'),
          ),
          '#default_value' => variable_get('fblikebutton_bl_show_faces', 'true'),
          '#description' => t('Show profile pictures below the button. Only works if <em>Layout style</em> found above is set to <em>Standard</em> (otherwise, value is ignored).'),
        );
        $form['fblikebutton_block']['fblikebutton_bl_action'] = array(
          '#type' => 'select',
          '#title' => t('Verb to display'),
          '#options' => array(
            'like' => t('Like'),
            'recommend' => t('Recommend'),
          ),
          '#default_value' => variable_get('fblikebutton_bl_action', 'like'),
          '#description' => t('The verbiage to display inside the button itself.'),
        );
        $form['fblikebutton_block']['fblikebutton_bl_font'] = array(
          '#type' => 'select',
          '#title' => t('Font'),
          '#options' => array(
            'arial' => 'Arial',
            'lucida+grande' => 'Lucida Grande',
            'segoe+ui' => 'Segoe UI',
            'tahoma' => 'Tahoma',
            'trebuchet+ms' => 'Trebuchet MS',
            'verdana' => 'Verdana',
          ),
          '#default_value' => variable_get('fblikebutton_bl_font', 'arial'),
          '#description' => t('The font with which to display the text of the button.'),
        );
        $form['fblikebutton_block']['fblikebutton_bl_color_scheme'] = array(
          '#type' => 'select',
          '#title' => t('Color scheme'),
          '#options' => array(
            'light' => t('Light'),
            'dark' => t('Dark'),
          ),
          '#default_value' => variable_get('fblikebutton_bl_color_scheme', 'light'),
          '#description' => t('The color scheme of the box environtment.'),
        );
        $form['fblikebutton_block']['fblikebutton_bl_language'] = array(
          '#type' => 'textfield',
          '#title' => t('Language'),
          '#default_value' => variable_get('fblikebutton_bl_language', $fblikebutton_block_lang),
          '#description' => t('Specific language to use. Default for this site is <em>@lang</em>. Examples:<br/>French (France): <em>fr_FR</em><br/>French (Canada): <em>fr_CA</em>', array(
            '@lang' => $fblikebutton_block_lang,
          )),
        );
      }
      return $form;
    case 'save':
      if ($delta == 0) {
        variable_set('fblikebutton_bl_layout', $edit['fblikebutton_bl_layout']);
        variable_set('fblikebutton_bl_show_faces', $edit['fblikebutton_bl_show_faces']);
        variable_set('fblikebutton_bl_action', $edit['fblikebutton_bl_action']);
        variable_set('fblikebutton_bl_font', $edit['fblikebutton_bl_font']);
        variable_set('fblikebutton_bl_color_scheme', $edit['fblikebutton_bl_color_scheme']);
        variable_set('fblikebutton_bl_language', $edit['fblikebutton_bl_language']);
      }
      break;
    case 'view':
      global $base_url, $language;
      $block_lang_abbr = $language->language;
      $fblikebutton_block_lang = $block_lang_abbr . '_' . drupal_strtoupper($block_lang_abbr);
      $addr = variable_get('fblikebutton_block_url', $base_url);
      $conf = array(
        'layout' => variable_get('fblikebutton_bl_layout', "standard"),
        'action' => variable_get('fblikebutton_bl_action', "like"),
        'color_scheme' => variable_get('fblikebutton_bl_color_scheme', "light"),
        'show_faces' => variable_get('fblikebutton_bl_show_faces', "false"),
        'font' => variable_get('fblikebutton_bl_font', "arial"),
        'language' => variable_get('fblikebutton_bl_language', $fblikebutton_block_lang),
      );
      if (user_access('users may access Like button')) {
        $block['subject'] = t('');
        $block['content'] = _fblikebutton_field($addr, $conf);
        return $block;
      }
  }
}
function _fblikebutton_field($addr, $conf) {
  $addr = urlencode($addr);
  $width = "100%";
  $layout = $conf['layout'];
  $action = $conf['action'];
  $colorscheme = $conf['color_scheme'];
  $show_faces = $conf['show_faces'];
  $font = $conf['font'];
  $lang = $conf['language'];
  switch ($layout) {
    case "box_count":
      $height = 65;
    case "button_count":
      $height = 21;
    case "standard":
    default:
      $height = $show_faces == "false" ? 35 : 80;
  }
  $height = 60;
  $params = "href={$addr}&layout={$layout}&show_faces=false&width&font={$font}&height={$height}&action={$action}&colorscheme={$colorscheme}&locale={$lang}";
  $src = htmlentities($params);
  $output = "<iframe src=\"https://www.facebook.com/plugins/like.php?{$src}\" scrolling=\"no\" frameborder=\"0\" style=\"border: none; overflow: hidden; width: {$width}; height: {$height}px;\" allowTransparency=\"true\"></iframe>";
  return $output;
}

Functions

Namesort descending Description
fblikebutton_block Implementation of hook_block().
fblikebutton_menu Implementation of hook_menu().
fblikebutton_nodeapi Implementation of hook_nodeapi().
fblikebutton_perm Implementation of hook_perm().
_fblikebutton_field