By darrellhq on
have tweaked the tweetmeme module so all credit goes to the author of that module - Rob Loach. This uses that code in the module to embed the facebook share button into the node. Simple enough however I would like for either the author of tweetmeme module or another more seasoned developer than myself to take a look. There are a few things that still do not work in the Drupal backend however it will take the settings and seems to work fine for sharing website content on FaceBook.
facebookshare.install:
/**
* Implementation of hook_uninstall().
*/
function facebookshare_uninstall() {
variable_del('facebookshare_types');
variable_del('facebookshare_location');
variable_del('facebookshare_style');
}
facebookshare.admin.inc:
function facebookshare_admin_settings() {
$form['facebookshare_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Content types'),
'#description' => t('Which content types to apply the facebookshare button to.'),
'#options' => node_get_types('names'),
'#default_value' => variable_get('facebookshare_types', array()),
);
$form['facebookshare_location'] = array(
'#type' => 'checkboxes',
'#title' => t('Location'),
'#description' => t('Where to show the facebookshare button.'),
'#options' => array(
'content' => t('Full view'),
'teasers' => t('Teasers'),
),
'#default_value' => variable_get('facebookshare_location', array()),
);
$form['facebookshare_style'] = array(
'#type' => 'select',
'#title' => t('Style'),
'#description' => t('The style of the button to use.'),
'#options' => array(
'normal' => t('box_count'),
'compact' => t('button_count'),
),
'#default_value' => variable_get('facebookshare_style', 'normal'),
);
return system_settings_form($form);
}
facebookshare.module:
function facebookshare_help($path, $arg) {
switch ($path) {
case 'admin/settings/facebookshare':
case 'admin/help#facebookshare':
return '<p>'. t('<a href="@facebookshare">facebookshare</a> provides a way of tracking popular links on <a href="@facebookshare">Facebook</a>.', array('@facebookshare' => 'http://facebook.com', '@facebookshare' => 'http://facebook.com')) .'</p>';
}
}
/**
* Implementation of hook_perm().
*/
function facebookshare_perm() {
return array(
'administer facebookshare',
'access facebookshare',
);
}
/**
* Implementation of hook_menu().
*/
function facebookshare_menu() {
$items['admin/settings/facebookshare'] = array(
'title' => 'facebookshare',
'description' => 'Provides the configuration options for how facebookshare 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 $items;
}
/**
* Implementation of hook_nodeapi().
*/
function facebookshare_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
if ($op == 'view') {
// Make sure we're on the right content type.
if (!in_array($node->type, variable_get('facebookshare_types', array()), TRUE)) {
return NULL;
}
// Make sure we're actually building the page to render in a browser.
if ($node->build_mode != NODE_BUILD_NORMAL) {
return NULL;
}
// Make sure the user has access to use TweetMeme.
if (!user_access('access facebookshare')) {
return NULL;
}
// Retrieve the location where we should show it, the style and the URL of the button.
$location = variable_get('facebookshare_location', array());
$style = variable_get('facebookshare_style', 'normal');
$url = url('node/' . $node->nid, array('absolute' => TRUE));
// Check in the teaser view.
if ($teaser && !empty($location['teasers'])) {
$node->content['facebookshare'] = array(
'#value' => theme('facebookshare_button', $url, $style),
'#weight' => -10,
);
}
// Check in the full view.
else if(!$teaser && !empty($location['content'])) {
$node->content['facebookshare'] = array(
'#value' => theme('facebookshare_button', $url, $style),
'#weight' => -10,
);
}
}
}
/**
* Implementation of hook_theme().
*/
function facebookshare_theme($existing, $type, $theme, $path) {
return array(
'facebookshare_button' => array(
'arguments' => array(
'url' => NULL,
'style' => NULL,
),
),
);
}
/**
* Displays the facebookshare button
*/
function theme_facebookshare_button($url, $style = 'normal') {
drupal_add_css(drupal_get_path('module', 'facebookshare') . '/facebookshare.css');
$output = '<div class="facebookshare facebookshare-button"><a name="fb_share" type="';
if ($style != 'normal') {
$output .= "facebookshare_style = '$style';";
}
$output .= ' href="http://www.facebook.com/sharer.php">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script></div>';
return $output;
}
Comments
Thank you!
Thanks for the work, by publishing it here you've saved me a heap of time having to do it myself.
Haven't really looked at the code from a "standards" point of view. You're missing the .info file and .css, but that's not really much of a drama
I'm playing with the code now. Don't like the position, but that can be fixed with theming. It seems to work when you run this from a public site, but not when you are testing from localhost.
I like it. I'm going to use it!
No problem here are the two
No problem here are the two missing files
facebookshare.info
facebookshare.css
I'd like to see some come on to address the issues you mentioned. I have this running on my live site. www.fevermagazine.com