function addtoany_page_alter in AddToAny Share Buttons 7.4
Same name and namespace in other branches
- 5.0 addtoany.module \addtoany_page_alter()
- 7 addtoany.module \addtoany_page_alter()
- 7.3 addtoany.module \addtoany_page_alter()
Implements hook_page_alter().
Return value
String containing JavaScript code for asynchronous loading of AddToAny's external script.
File
- ./
addtoany.module, line 149 - Stand alone module file to handle AddToAny buttons integration
Code
function addtoany_page_alter(&$page) {
global $_addtoany_init, $_addtoany_targets;
// Always add CSS file (necessary for aggregation)
$page['content']['#attached']['css'][] = drupal_get_path('module', 'addtoany') . '/addtoany.css';
// Only output script when AddToAny is used
if (!$_addtoany_init) {
return;
}
$javascript_header = "window.a2a_config=window.a2a_config||{};" . "window.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='https://static.addtoany.com/menu/page.js';" . "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});" . "el.id='';" . "}" . "da2a.done=true;" . "}" . "da2a.targets=[];" . "}" . "};" . "(function (\$){" . "Drupal.behaviors.addToAny = {" . "attach: function (context, settings) {" . "if (context !== document && window.da2a) {" . "if(da2a.script_ready)a2a.init_all();" . "da2a.script_load();" . "}" . "}" . "}" . "})(jQuery);" . "a2a_config.callbacks=a2a_config.callbacks||[];" . "a2a_config.callbacks.push({ready:da2a.script_onready});" . "a2a_config.overlays=a2a_config.overlays||[];" . "a2a_config.templates=a2a_config.templates||{};" . (variable_get('addtoany_no_3p', '0') != '1' ? "" : "a2a_config.no_3p=1;") . 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 instance. Fixes issues where first instance code is processed internally but without actual code output
$page['content']['#attached']['js'][] = array(
'data' => $javascript_header,
'scope' => 'header',
'type' => 'inline',
);
$page['content']['#attached']['js'][] = array(
'data' => $javascript_footer,
'scope' => 'footer',
'type' => 'inline',
);
/**
* Custom CSS Setting
*/
$css = variable_get('addtoany_additional_css', '');
if (!empty($css)) {
drupal_add_css($css, 'inline');
}
}