You are here

facebookshare.module in Facebook Share 7

Same filename and directory in other branches
  1. 8 facebookshare.module
  2. 6.3 facebookshare.module
  3. 6 facebookshare.module

Adds a button to a node to share on a user's Facebook stream

File

facebookshare.module
View source
<?php

/**
 * @file
 * Adds a button to a node to share on a user's Facebook stream
 */

/**
 * Implements hook_help()
 */
function facebookshare_help($path, $arg) {
  switch ($path) {
    case 'admin/config/user-interface/facebookshare':
    case 'admin/help#facebookshare':
      return '<p>' . t('Provides a way for viewers to share a link to a node on' . ' their Facebook stream.') . '</p>';
  }
}

/**
 * Implements hook_permission()
 */
function facebookshare_permission() {
  return array(
    'administer facebookshare' => array(
      'title' => t('administer facebookshare'),
    ),
    'access facebookshare' => array(
      'title' => t('access facebookshare'),
    ),
  );
}

/**
 * Implements hook_menu()
 */
function facebookshare_menu() {
  $item['admin/config/user-interface/facebookshare'] = array(
    'title' => 'Facebook Share',
    'description' => 'Provides the configuration options for how Facebook Share operates on the site.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'facebookshare_admin_settings',
    ),
    'access arguments' => array(
      'administer facebookshare',
    ),
    'file' => 'facebookshare.admin.inc',
  );
  return $item;
}

/**
 * Implements hook_init()
 */
function facebookshare_init() {
  drupal_add_css(drupal_get_path('module', 'facebookshare') . '/facebookshare.css');
}

/**
 * Implements hook_node_view()
 */
function facebookshare_node_view($node, $view_mode = 'full') {

  // Make sure we're on the right content type.
  if (!in_array($node->type, variable_get('facebookshare_types', array()), TRUE)) {
    return NULL;
  }

  // We only render the button on full pages and on teasers
  if ($view_mode != 'teaser' && $view_mode != 'full') {
    return NULL;
  }

  // Make sure the user has access to use Facebook Share.
  if (!user_access('access facebookshare')) {
    return NULL;
  }

  // Retrieve the location where we should show it
  $location = variable_get('facebookshare_location', array());

  // Check if the current $view_mode has content to show
  if ($view_mode == 'teaser' && !empty($location['teasers']) || $view_mode == 'full' && !empty($location['content'])) {

    // Retrieve the weight and url of the button
    $url = url('node/' . $node->nid, array(
      'absolute' => TRUE,
    ));
    $weight = check_plain(variable_get('facebookshare_weight', -10));

    // Add and theme the button
    $node->content['facebookshare'] = array(
      '#markup' => theme('facebookshare', array(
        'url' => $url,
      )),
      '#weight' => is_numeric($weight) ? (int) $weight : -10,
    );
  }
}

/**
 * Implements hook_theme()
 */
function facebookshare_theme($existing, $type, $theme, $path) {
  return array(
    'facebookshare' => array(
      'variables' => array(
        'url' => NULL,
      ),
    ),
    'facebookshare_button' => array(
      'variables' => array(
        'url' => NULL,
        'size' => NULL,
        'text' => NULL,
      ),
    ),
  );
}

/**
 * Themes facebook share button box
 */
function theme_facebookshare($variables) {
  $url = $variables['url'];
  $output = '<div class="facebookshare-box">';
  $output .= theme('facebookshare_button', array(
    'url' => $url,
    'size' => check_plain(variable_get('facebookshare_size', '')),
    'text' => check_plain(variable_get('facebookshare_text', '')),
  ));
  $output .= '</div>';
  return $output;
}

/**
 * Themes facebook share button
 */
function theme_facebookshare_button($variables) {
  $url = $variables['url'];
  $size = $variables['size'];
  $text = $variables['text'];
  $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
  $script_url = $protocol == 'https' ? 'https://s-static.ak.fbcdn.net/connect.php/js/FB.Share' : 'http://static.ak.fbcdn.net/connect.php/js/FB.Share';
  $output = '<a name="fb_share" ' . 'type="' . $size . '" share_url="' . $url . '">' . $text . '</a>' . '<script src="' . $script_url . '" type="text/javascript"></script>';
  return $output;
}

Functions

Namesort descending Description
facebookshare_help Implements hook_help()
facebookshare_init Implements hook_init()
facebookshare_menu Implements hook_menu()
facebookshare_node_view Implements hook_node_view()
facebookshare_permission Implements hook_permission()
facebookshare_theme Implements hook_theme()
theme_facebookshare Themes facebook share button box
theme_facebookshare_button Themes facebook share button