Commit e4a40aa2 authored by Riccardo Padovani's avatar Riccardo Padovani

Added fblikebutton module

parent 3d7d601c
FB Like Button 7.x-2.x, xxxx-xx-xx
-----------------------------
by lliss: make config array generation into a single funciton and fix issue on the admin page for static button language selection.
#1437354 by kbemelmans and jonhattan - Adding display suite integration.
by lliss: Add better help text for localization options to the config pages.
#1668256 by goye2cz: Fixed the broken language setting on the dynamic fblike button.
by lliss make the send attribute explicit. See iframe code generated at http://developers.facebook.com/docs/reference/plugins/like/ for more info.
#1570374 by jgtrescazes, Stakcreativa: Fixes the display issue caused by the iframe having 'px' string after the width and height values. Also makes the language parameter optional.
#1664410 by lliss: Fixed the display failure of the fb button by changing the show_faces options that are stored as drupal variables to booleans and then outputting 'true' and 'false' text when generating the ifrome.
This diff is collapsed.
README.txt
==========
Drupal Facebook Like Button module:
------------------------
Requires - Drupal 7
License - GPL (see LICENSE)
Overview:
--------
Rather than having to manually copy/paste the "Like this on Facebook" code for
each piece of content you (or your users) create, this module will automatically
add that code to the end of each chosen node type. You are able to add a Like
button which likes a given URL (static, e.g. your homepage) or/and put a dynamic
Like button on your page to like the page you are actually visiting.
Features:
---------
The Facebook like button module:
* Adds a like button on your site (static)
* Add more like buttons to your site (dynamic)
* Control the position of the buttons
* Integrates with display suite.
* Overrideable output through templates and preprocess functions.
Installation:
------------
Install this module in the usual way (drop the file into your site's contributed modules directory).
Configuration:
-------------
Go to "Configuration" -> "FB LIKE BUTTON" to find
all the configuration options.
Overriding Output:
-----------------
Copy the fblikebutton.tpl.php to your theme's directory and alter at will. This
should be necessary unless the like button stops working because Facebook has
changed their service. You can also override the output by creating a
preprocess function in your theme's template.php file.
Display Suite Integration:
-------------------------
When using display suite to display the node's content the normal Facebook like
button configuration settings for the dynamic Facebook like button become
somewhat irrelevant.
This diff is collapsed.
; $Id$
name = Facebook Like Button
description = Adds a configurable <em>Like</em> button for Facebook to each selected node type, as well as a configurable block with a <em>Like</em> box in it.
core = 7.x
configure = admin/config/fblikebutton
; Information added by drupal.org packaging script on 2012-11-26
version = "7.x-2.3"
core = "7.x"
project = "fblikebutton"
datestamp = "1353948751"
<?php
/**
* @file
* Install, update, and uninstall functions for the fblikebutton module.
*/
// $Id$
/**
* Implementation of hook_install().
*/
function fblikebutton_install() {
// There really is no "schema" to install.
}
/**
* Implementation of hook_uninstall().
*/
function fblikebutton_uninstall() {
variable_del('fblikebutton_full_node_display');
variable_del('fblikebutton_bl_iframe_css');
variable_del('fblikebutton_node_types');
variable_del('fblikebutton_show_faces');
variable_del('fblikebutton_layout');
variable_del('fblikebutton_color_scheme');
variable_del('fblikebutton_iframe_height');
variable_del('fblikebutton_iframe_width');
variable_del('fblikebutton_iframe_css');
variable_del('fblikebutton_language');
variable_del('fblikebutton_displaysend');
variable_del('fblikebutton_font');
variable_del('fblikebutton_weight');
variable_del('fblikebutton_teaser_display');
variable_del('fblikebutton_xfbml');
variable_del('fblikebutton_appid');
variable_del('fblikebutton_bl_layout');
variable_del('fblikebutton_bl_layout');
variable_del('fblikebutton_bl_show_faces');
variable_del('fblikebutton_bl_action');
variable_del('fblikebutton_bl_font');
variable_del('fblikebutton_bl_color_scheme');
variable_del('fblikebutton_bl_iframe_width');
variable_del('fblikebutton_bl_iframe_height');
variable_del('fblikebutton_bl_iframe_css');
variable_del('fblikebutton_bl_language');
}
/**
* Change the stored variables for the 'show faces' option to a simple boolean.
*/
function fblikebutton_update_7200() {
// Update the variables for the dynamic fblikebutton.
$faces = variable_get('fblikebutton_show_faces', '');
if ($faces == 'show') {
variable_set('fblikebutton_show_faces', TRUE);
}
else if ($faces == 'hide') {
variable_set('fblikebutton_show_faces', FALSE);
}
// Update the variables for the static fblikebutton.
$faces = variable_get('fblikebutton_bl_show_faces', '');
if ($faces == 'show') {
variable_set('fblikebutton_bl_show_faces', TRUE);
}
else if ($faces == 'hide') {
variable_set('fblikebutton_bl_show_faces', FALSE);
}
}
/**
* Remove old values and replace with properly-formatted values, as some sites are experiencing problems from previous update.
*/
function fblikebutton_update_7201() {
// Just delete the variable and inform the administrator that the value has been set to false, and if it should be true, then they need to manually set it to true.
// Too much confusion has occurred with the word "true" and the PHP boolean TRUE, and it's causing the button not to show up on some sites.
variable_del('fblikebutton_show_faces');
variable_set('fblikebutton_show_faces', TRUE);
variable_del('fblikebutton_bl_show_faces');
variable_set('fblikebutton_bl_show_faces', TRUE);
cache_clear_all();
drupal_set_message(t('The Facebook Like Button has been updated successfully. However, if you previously set the values of "show faces" to "Do not show faces", you will need to set those values back to "Do not show faces" now by visiting the settings page for both the') . ' ' . l(t('static'), 'admin/config/fblikebutton/static') . ' ' . t('and the') . ' ' . l(t('dynamic'), 'admin/config/fblikebutton/dynamic') . ' ' . t('settings pages for the Facebook Like Button.'));
}
<?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.
*/
/**
* Implements of hook_menu().
*/
function fblikebutton_menu() {
$items['admin/config/fblikebutton'] = array(
'title' => 'FB Like Button',
'description' => 'Configure the Facebook Like button',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer fblikebutton'),
'position' => 'right',
'weight' => -20,
'type' => MENU_NORMAL_ITEM,
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/fblikebutton/dynamic'] = array(
'title' => 'Dynamic Like button settings',
'description' => 'Configure the settings for the Like button as it appears on individual nodes (liking that node).',
'page callback' => 'drupal_get_form',
'page arguments' => array('fblikebutton_dynamic_settings'),
'access arguments' => array('administer fblikebutton'),
'type' => MENU_NORMAL_ITEM,
'file' => 'fblikebutton.admin.inc',
'weight' => 0,
);
$items['admin/config/fblikebutton/static'] = array(
'title' => 'Static Like button settings',
'description' => 'Configure the settings for the static Like button as it appears in the block (liking a given url). Use this to like for example your Facebook fanpage.',
'page callback' => 'drupal_get_form',
'page arguments' => array('fblikebutton_static_settings'),
'access arguments' => array('administer fblikebutton block'),
'type' => MENU_NORMAL_ITEM,
'file' => 'fblikebutton.admin.inc',
'weight' => 1,
);
return $items;
}
/**
* Helper function to generate the configuration array used to generate the like
* button.
*/
function fblikebutton_conf($type = 'dynamic') {
$prefix = 'fblikebutton';
if ($type == 'static') {
$prefix = 'fblikebutton_bl';
}
$conf = array(
'layout' => variable_get($prefix . '_layout', 'standard'),
'action' => variable_get($prefix . '_action', 'like'),
'color_scheme' => variable_get($prefix . '_color_scheme', 'light'),
'show_faces' => variable_get($prefix . '_show_faces', 'true'),
'font' => variable_get($prefix . '_font', 'arial'),
'height' => variable_get($prefix . '_iframe_height', '80'),
'width' => variable_get($prefix . '_iframe_width', '450'),
'send' => variable_get($prefix . '_send', 'true'),
'other_css' => variable_get($prefix . '_iframe_css', ''),
'language' => variable_get($prefix . '_language', 'en_US'),
);
return $conf;
}
/**
* Implements of hook_node_view().
*/
function fblikebutton_node_view($node, $view_mode) {
global $user, $base_url;
$types = variable_get('fblikebutton_node_types', array());
$fullnodedisplay = variable_get('fblikebutton_full_node_display', 0);
$teaserdisplay = variable_get('fblikebutton_teaser_display', 0);
$full = ($view_mode == 'full') ? TRUE : FALSE;
$show = ( ! empty($types[$node->type]) && user_access('access fblikebutton'));
$webpage_to_like = url("node/$node->nid", array('absolute' => TRUE));
$likebutton_weight = variable_get('fblikebutton_weight', '0');
$conf = fblikebutton_conf();
if ($show) {
// Content area
if (($view_mode == 'teaser' && $teaserdisplay == 1) || ($view_mode == 'full' && $fullnodedisplay == 0)) {
$node->content['fblikebutton_field'] = array(
'#markup' => _fblikebutton_field($webpage_to_like, $conf),
'#weight' => $likebutton_weight,
);
}
// Link area
if (($view_mode == 'teaser' && $teaserdisplay == 2) || ($view_mode == 'full' && $fullnodedisplay == 2)) {
$node->content['links']['#links']['fblikebutton_field'] = array(
'title' => _fblikebutton_field($webpage_to_like, $conf),
'html' => TRUE,
);
}
}
}
/**
* Implements of hook_permission().
*/
function fblikebutton_permission() {
return array(
'administer fblikebutton' => array(
'title' => t('Administer FB Like button'),
'description' => t('Perform administration tasks for FB Like button')
),
'administer fblikebutton block' => array(
'title' => t('Administer FB Like button block'),
'description' => t('Perform administration tasks for FB Like button block')
),
'access fblikebutton' => array(
'title' => t('Access FB Like button'),
),
);
}
/**
* Implementation of hook_block_info().
*/
function fblikebutton_block_info() {
$fullnodedisplay = variable_get('fblikebutton_full_node_display', 0);
$blocks['fblikebutton_static_block'] = array(
'info' => t('Static FB Like button'),
'cache' => DRUPAL_NO_CACHE,
);
if ($fullnodedisplay == 1) {
$blocks['fblikebutton_dynamic_block'] = array(
'info' => t('Dynamic FB Like button'),
'cache' => DRUPAL_NO_CACHE,
);
}
return $blocks;
}
/**
* Implementation of hook_block_configure().
*/
function fblikebutton_block_configure($delta = '') {
global $base_url;
$form = array();
if ($delta == 'fblikebutton_static_block') {
$form['fblikebutton_static_block'] = array(
'#type' => 'fieldset',
'#title' => t('Static FB Like button block'),
'#collapsible' => FALSE,
);
$form['fblikebutton_static_block']['fblikebutton_static_config'] = array(
'#markup' => '<p>' . t('To configure the URL and the appearance of the button go to the ' . l(t('static Like button settings'), 'admin/config/fblikebutton/static') . '. Make sure you set the right permissions on the ' . l(t('permissions page'), 'admin/people/permissions') . '.</p>'),
);
}
if ($delta == 'fblikebutton_dynamic_block') {
$form['fblikebutton_dynamic_block'] = array(
'#type' => 'fieldset',
'#title' => t('Dynamic FB Like button block'),
'#collapsible' => FALSE,
);
$form['fblikebutton_dynamic_block']['fblikebutton_dynamic_config'] = array(
'#markup' => '<p>' . t('To configure the visibility and the appearance of the button go to the ' . l(t('dynamic Like button settings'), 'admin/config/fblikebutton/dynamic') . '. You can enhance the visibility settings by using the settings on this page. Make sure you set the right permissions on the ' . l(t('permissions page'), 'admin/people/permissions') . '.</p>'),
);
}
return $form;
}
/**
* Implementation of hook_block_view().
*/
function fblikebutton_block_view($delta = '') {
global $base_url;
$node = node_load(arg(1));
$types = variable_get('fblikebutton_node_types', array());
if ($node) {
$show = ( ! empty($types[$node->type]) && user_access('access fblikebutton'));
}
else {
$show = NULL;
}
$fullnodedisplay = variable_get('fblikebutton_full_node_display', 0);
$block = array();
switch ($delta) {
case 'fblikebutton_dynamic_block':
if ($show && $fullnodedisplay == 1) {
$webpage_to_like = url("node/$node->nid", array('absolute' => TRUE));
$conf = fblikebutton_conf();
$block['content'] = _fblikebutton_field($webpage_to_like, $conf);
}
break;
case 'fblikebutton_static_block':
$addr = variable_get('fblikebutton_block_url', $base_url);
$conf = fblikebutton_conf('static');
$block['content'] = _fblikebutton_field($addr, $conf);
break;
}
return $block;
}
/**
* Implements hook_ds_fields_info().
*/
function fblikebutton_ds_fields_info($entity_type) {
$fields = array();
if ($entity_type == 'node') {
$ui_limit = array();
$bundles = variable_get('fblikebutton_node_types', array());
foreach ($bundles as $bundle) {
$ui_limit[] = $bundle . '|*';
}
$fields['node'] = array();
$fields['node']['fblikebutton_ds_field'] = array(
'title' => t('Dynamic Facebook Like button'),
'field_type' => DS_FIELD_TYPE_FUNCTION,
'function' => '_fblikebutton_ds_field',
'ui_limit' => $ui_limit,
);
}
return $fields;
}
/**
* Callback for ds button.
*/
function _fblikebutton_ds_field($field) {
if (!user_access('access fblikebutton')) {
return '';
}
$uri = entity_uri($field['entity_type'], $field['entity']);
$url = url($uri['path'], array('absolute' => TRUE));
$conf = fblikebutton_conf();
return _fblikebutton_field($url, $conf);
}
/**
* Build the like button. This is essentially just a convienience function that
* appends the url to the configuration array.
*/
function _fblikebutton_field($webpage_to_like, $conf) {
$conf['url'] = $webpage_to_like;
return theme('fblikebutton', $conf);
}
/**
* Implements hook_theme().
*/
function fblikebutton_theme() {
$theme = array();
$theme['fblikebutton'] = array(
'variables' => fblikebutton_conf('static'),
'file' => 'fblikebutton.theme.inc',
'template' => 'fblikebutton',
);
return $theme;
}
<?php
/**
* Implements template_preprocess().
*
* Formats variables and sanitizes data needed to build the like button.
*/
function template_preprocess_fblikebutton(&$variables) {
$url = urlencode($variables['url']);
$language = ($variables['language']) ? '&locale=' . $variables['language'] : '';
// Ensure we translate the boolean into a string for the url to pass to
// facebook.
$show_faces = $variables['show_faces'] ? 'true' : 'false';
// We run the config src line through htmlentities to avoid rendering trouble
// when we print to the page.
$variables['src'] = htmlentities(sprintf('href=%s&layout=%s&show_faces=%s&width=%s&font=%s&height=%s&action=%s&colorscheme=%s%s&send=false',
$url,
$variables['layout'],
$show_faces,
$variables['width'],
$variables['font'],
$variables['height'],
$variables['action'],
$variables['color_scheme'],
$language
));
}
<?php
/**
* @file
* Template file for fblikebutton.
*
* The variable of primary importance is $src. This is generated and sanitized
* the preprocess function. As facebook's services change frequently you can
* override these settings without altering this module by overriding the
* preprocess function in your theme's template.php file. The overriding
* preprocessor should be a function named MYTHEME_preprocess_fblikebutton. You
* can also override the output of this file by copying it into your own theme
* directory. For maintainability you should not alter this file on your site as
* it will lead to complications when updating the module later.
*
*/
?>
<iframe src="//www.facebook.com/plugins/like.php?<?php print $src; ?>" scrolling="no" frameborder="0" style="border: none; overflow: hidden; width: <?php print $width; ?>px; height: <?php print $height; ?>px; <?php $other_css; ?>" allowTransparency="true"></iframe>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment