Skip to content

Instantly share code, notes, and snippets.

@jrbeeman
Created June 24, 2014 20:14
Show Gist options
  • Save jrbeeman/8e59a1e271428eb490d1 to your computer and use it in GitHub Desktop.
Save jrbeeman/8e59a1e271428eb490d1 to your computer and use it in GitHub Desktop.
Example of simple custom sharing widget module in Drupal
<?php
/**
* @file
* Watercooler: Share module.
*
* Twitter widget generated at: https://about.twitter.com/resources/buttons#tweet
* Facebook widget generated at: https://developers.facebook.com/docs/plugins/like-button/
* Google+ widget generated at: https://developers.google.com/+/web/+1button/
*
* The RSS feed URL can be overridden via the site variable 'wc_share_rss_url'
*/
/**
* Implements hook_block_info().
*/
function wc_share_block_info() {
$blocks = array();
$blocks['widgets'] = array(
'info' => t('Watercooler: Share widgets'),
'cache' => DRUPAL_CACHE_PER_PAGE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function wc_share_block_view($delta = '') {
// Adds dependent JavaScript to the page.
switch ($delta) {
case 'widgets':
return wc_share_block_widgets();
}
}
/**
* Create the widgets block.
*
* @todo Move markup to template file.
*/
function wc_share_block_widgets() {
$widgets = array();
$widgets['twitter'] = wc_share_twitter_content();
$widgets['facebook'] = wc_share_facebook_content();
$widgets['googleplus'] = wc_share_googleplus_content();
$widgets['rss'] = wc_share_rss_content();
// RSS widget (defaults to Drupal core's rss.xml).
// Build and return the render-able block.
$content = '';
foreach ($widgets as $key => $value) {
$content .= '<div class="wc-share-widget wc-share-' . $key . '">' . $value . '</div>';
}
$block = array(
'subject' => NULL,
'content' => $content,
);
return $block;
}
/**
* Return Twitter widget markup.
*/
function wc_share_twitter_content() {
$markup = <<<HTML
<a href="https://twitter.com/share" class="twitter-share-button" data-count="none">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
HTML;
return $markup;
}
/**
* Return Facebook widget markup.
*/
function wc_share_facebook_content() {
$markup = <<<HTML
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-like" data-href="https://developers.facebook.com/docs/plugins/" data-layout="button" data-action="like" data-show-faces="false" data-share="false"></div>
HTML;
return $markup;
}
/**
* Return Google Plus widget markup.
*/
function wc_share_googleplus_content() {
$markup = <<<HTML
<!-- Place this tag where you want the +1 button to render. -->
<div class="g-plusone" data-size="medium" data-annotation="none"></div>
<!-- Place this tag after the last +1 button tag. -->
<script type="text/javascript">
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/platform.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
HTML;
return $markup;
}
/**
* Return RSS widget markup.
*/
function wc_share_rss_content() {
$site_name = variable_get('site_name');
$feed_url = variable_get('wc_share_rss_url', 'rss.xml');
$markup = theme('feed_icon', array('url' => $feed_url, 'title' => $site_name));
return $markup;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment