addtoany.module in AddToAny Share Buttons 7
Same filename and directory in other branches
Stand alone module file to handle AddToAny button integration
File
addtoany.moduleView source
<?php
/**
* @file
* Stand alone module file to handle AddToAny button integration
*/
/**
* Implements hook_install().
*/
function addtoany_install() {
drupal_set_message(st("Your AddToAny settings are available under !link", array(
'!link' => l('Configuration > System > AddToAny ', 'admin/config/system/addtoany'),
)));
}
/**
* Implementation of hook_permission().
*/
function addtoany_permission() {
return array(
'administer addtoany' => array(
'title' => t('Administer addtoany'),
'description' => t(''),
),
);
}
/**
* Implementation of hook_node_view().
*/
function addtoany_node_view($node, $view_mode) {
$links = array();
$types = variable_get('addtoany_nodetypes', array());
$teaser = $view_mode == 'teaser' ? TRUE : FALSE;
$show = !empty($types[$node->type]) && ($teaser && variable_get('addtoany_display_in_teasers', '1') != 0 || !$teaser && variable_get('addtoany_display_in_nodelink', '1') != 0);
if ($show) {
$node->content['links']['#links']['addtoany'] = array(
'title' => _addtoany_create_button($node, $teaser),
'html' => TRUE,
);
}
$show = !empty($types[$node->type]) && variable_get('addtoany_display_in_nodecont', '0');
$weight = variable_get('addtoany_display_weight', 40);
if ($show) {
$node->content['addtoany'] = array(
'#markup' => _addtoany_create_button($node, FALSE),
'#weight' => $weight,
);
}
}
/**
* Implementation of hook_views_api().
*/
function addtoany_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'addtoany') . '/views',
);
}
/**
* Implementation of hook_menu().
*/
function addtoany_menu() {
$items = array();
$items['admin/config/system/addtoany'] = array(
'title' => t('AddToAny'),
'description' => t('Settings for your <a href="http://www.addtoany.com/" target="blank">AddToAny</a> Share/Save buttons.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'addtoany_admin_settings',
),
'access arguments' => array(
'administer addtoany',
),
'file' => 'addtoany.admin.inc',
);
return $items;
}
/**
* Implements hook_block_info().
*/
function addtoany_block_info() {
$blocks['addtoany_button']['info'] = t('AddToAny button');
return $blocks;
}
/**
* Implements hook_block_list().
*/
function addtoany_block_list() {
$blocks['addtoany_button'] = array(
'info' => t('AddToAny button'),
'status' => TRUE,
'weight' => 0,
'visibility' => 1,
'pages' => '*',
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function addtoany_block_view($delta = 0) {
$block = array();
switch ($delta) {
case 'addtoany_button':
$block['subject'] = t('AddToAny');
$block['content'] = _addtoany_create_button(menu_get_object());
break;
}
return $block;
}
/**
* Implementation of hook_page_alter(). Would use drupal_add_js, but external scripts are not well supported
*
* @return
* String containing JavaScript code for asynchronous loading of AddToAny's external script.
*/
function addtoany_page_alter(&$page) {
global $_addtoany_init, $_addtoany_targets;
// Only output script when AddToAny is used
if (!$_addtoany_init) {
return;
}
$script_url = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://static.addtoany.com/menu/page.js' : 'http://static.addtoany.com/menu/page.js';
$javascript_header = "var a2a_config=a2a_config||{}," . "da2a={done:false," . "html_done:false," . "script_ready:false," . "script_load:function(){" . "var a=document.createElement('script')," . "s=document.getElementsByTagName('script')[0];" . "a.type='text/javascript';a.async=true;" . "a.src='" . $script_url . "';" . "s.parentNode.insertBefore(a,s);" . "da2a.script_load=function(){};" . "}," . "script_onready:function(){" . "da2a.script_ready=true;" . "if(da2a.html_done)da2a.init();" . "}," . "init:function(){" . "for(var i=0,el,target,targets=da2a.targets,length=targets.length;i<length;i++){" . "el=document.getElementById('da2a_'+(i+1));" . "target=targets[i];" . "a2a_config.linkname=target.title;" . "a2a_config.linkurl=target.url;" . "if(el)a2a.init('page',{target:el});da2a.done=true;" . "}" . "}" . "};" . "a2a_config.tracking_callback=['ready',da2a.script_onready];" . variable_get('addtoany_additional_js', '');
$javascript_footer = "da2a.targets=[" . implode(",", $_addtoany_targets) . "];\n" . "da2a.html_done=true;" . "if(da2a.script_ready&&!da2a.done)da2a.init();" . "da2a.script_load();";
// Load external script if not already called with the first AddToAny button. Fixes issues where first button code is processed internally but without actual code output
drupal_add_js($javascript_header, array(
'type' => 'inline',
'scope' => 'header',
));
drupal_add_js($javascript_footer, array(
'type' => 'inline',
'scope' => 'footer',
));
}
/**
* Internal function to call the async script loading function right after the first AddToAny button, to utilize concurrent downloading and increase overall page-load performance.
*
* @param object $id_num
* @param object $link_name
* @param object $link_url
* @return
* String containing a JavaScript function call for asynchronous script loading right after the first button.
*/
function _addtoany_create_script($id_num, $link_name, $link_url) {
global $_addtoany_targets, $_addtoany_init;
if (!isset($_addtoany_targets)) {
$_addtoany_targets = array();
}
$_addtoany_targets[] = "\n{title:'" . check_plain($link_name) . "'," . "url:'" . check_plain($link_url) . "'}";
if (!$_addtoany_init) {
$javascript_load_early = "<script type=\"text/javascript\">\n" . "<!--//--><![CDATA[//><!--\n" . "da2a.script_load();" . "//--><!]]>\n" . "</script>";
}
else {
$javascript_load_early = "";
}
$_addtoany_init = TRUE;
return $javascript_load_early;
}
/**
* Internal function to generate code for AddToAny button
*
* @param object $node [optional]
* @param object $teaser [optional]
* @return
* String containing HTML code for the button
*/
function _addtoany_create_button($node = NULL, $teaser = FALSE) {
global $_addtoany_counter, $base_path;
$_addtoany_counter++;
if ($_addtoany_counter == 1) {
drupal_add_css(drupal_get_path('module', 'addtoany') . '/addtoany.css');
}
$disable_dropdown = variable_get('addtoany_dropdown_disabled', '0');
if (is_object($node)) {
$link_name = module_exists('page_title') ? page_title_page_get_title() : $node->title;
$link_url = url('node/' . $node->nid, array(
'absolute' => 1,
));
}
else {
// Front page
$link_name = module_exists('page_title') ? page_title_page_get_title() : variable_get('site_name', 'Drupal');
$link_url = url('<front>', array(
'absolute' => 1,
));
}
$javascript = $disable_dropdown ? '' : _addtoany_create_script($_addtoany_counter, $link_name, $link_url);
$button_setting = variable_get('addtoany_image', 'share_save_171_16.png|171|16');
if ($button_setting == "custom") {
$button_image = check_url(variable_get('addtoany_custom_image', ''));
$button_width = '';
$button_height = '';
}
elseif ($button_setting != "text") {
$button = explode('|', $button_setting);
$button_filename = $button[0];
$button_width = ' width="' . $button[1] . '"';
$button_height = ' height="' . $button[2] . '"';
$button_image = $base_path . drupal_get_path('module', 'addtoany') . '/images/' . $button_filename;
}
$button_text = check_plain(variable_get('addtoany_link_text', ''));
if (strlen($button_text) > 0) {
$button_text = ' ' . $button_text;
}
$button_innerHTML = $button_setting == "text" ? $button_text : sprintf('<img src="%s"%s%s %s/>%s', $button_image, $button_width, $button_height, filter_xss(variable_get('addtoany_image_attributes', 'alt="Share this"'), array()), $button_text);
return sprintf('
<a%s href="http://www.addtoany.com/share_save" id="da2a_%s">%s</a>
%s
', $disable_dropdown ? '' : ' class="da2a_button"', $_addtoany_counter, $button_innerHTML, $javascript);
}
Functions
Name | Description |
---|---|
addtoany_block_info | Implements hook_block_info(). |
addtoany_block_list | Implements hook_block_list(). |
addtoany_block_view | Implements hook_block_view(). |
addtoany_install | Implements hook_install(). |
addtoany_menu | Implementation of hook_menu(). |
addtoany_node_view | Implementation of hook_node_view(). |
addtoany_page_alter | Implementation of hook_page_alter(). Would use drupal_add_js, but external scripts are not well supported |
addtoany_permission | Implementation of hook_permission(). |
addtoany_views_api | Implementation of hook_views_api(). |
_addtoany_create_button | Internal function to generate code for AddToAny button |
_addtoany_create_script | Internal function to call the async script loading function right after the first AddToAny button, to utilize concurrent downloading and increase overall page-load performance. |