You are here

sharebar.module in ShareBar 8

Same filename and directory in other branches
  1. 6 sharebar.module
  2. 7.2 sharebar.module
  3. 7 sharebar.module

Allows site owner to add share buttons on their website

File

sharebar.module
View source
<?php

/**
 * @file
 * Allows site owner to add share buttons on their website
 */
use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;
define('SHAREBAR_CREDIT_URL', 'http://www.socialseo.com/drupal-development/sharebar-module.html');

/**
 * Implements hook_theme().
 */
function sharebar_theme() {
  return array(
    'sharebar_buttons_table' => array(
      'file' => 'sharebar.admin.inc',
      'variables' => array(
        'var' => '',
      ),
    ),
  );
}

/**
 * Implements hook_preprocess_page().
 */
function sharebar_preprocess_page(&$variables) {
  $args = explode('/', current_path());
  if ($args[0] != 'admin') {
    if ($args[0] == 'node' && is_numeric($args[1]) && $args[2] == '' || !($args[0] == 'node' && is_numeric($args[1])) && $args[1] != 'add' && $args[2] != 'edit' && $args[3] != 'edit' && \Drupal::config('sharebar.settings')
      ->get('sharebar_bar_pages_enabled')) {
      _drupal_add_css(drupal_get_path('module', 'sharebar') . '/css/sharebar.css');
      if (!\Drupal::config('sharebar.settings')
        ->get('sharebar_bar_oncontent')) {
        if (\Drupal::config('sharebar.settings')
          ->get('sharebar_bar_horizontal')) {
          $hori = 'TRUE';
        }
        else {
          $hori = 'FALSE';
        }
        $width = \Drupal::config('sharebar.settings')
          ->get('sharebar_bar_width');
        $swidth = \Drupal::config('sharebar.settings')
          ->get('sharebar_bar_swidth');
        $position = \Drupal::config('sharebar.settings')
          ->get('sharebar_bar_position');
        $leftoffset = \Drupal::config('sharebar.settings')
          ->get('sharebar_bar_leftoffset');
        $rightoffset = \Drupal::config('sharebar.settings')
          ->get('sharebar_bar_rightoffset');
        _drupal_add_js(drupal_get_path('module', 'sharebar') . '/js/sharebar.js');
        _drupal_add_js('jQuery(document).ready(function($) { $(\'.sharebar\').sharebar({horizontal:\'' . $hori . '\',swidth:\'' . $swidth . '\',minwidth:' . $width . ',position:\'' . $position . '\',leftOffset:' . $leftoffset . ',rightOffset:' . $rightoffset . '}); });', 'inline');
      }
    }
  }
}

/**
 * Preprocess variables for region.tpl.php
 */
function sharebar_preprocess_region(&$variables) {
  $args = explode('/', current_path());

  //print_r($args); die;
  $is_node = FALSE;
  if ($node = \Drupal::routeMatch()
    ->getParameter('node')) {
    if ($args[0] == 'node' && is_numeric($args[1]) && !isset($args[2])) {
      $is_node = TRUE;
    }
    $node_type = $node
      ->bundle();
  }
  if ($variables['region'] == 'sidebar_first') {
    if ($args[0] != 'admin') {
      if ($is_node && \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_posts_' . $node_type . '_enabled') || !($args[0] == 'node' && is_numeric($args[1])) && $args[1] != 'add' && $args[2] != 'edit' && $args[3] != 'edit' && \Drupal::config('sharebar.settings')
        ->get('sharebar_bar_pages_enabled')) {
        $strh = '';
        $sbg = \Drupal::config('sharebar.settings')
          ->get('sharebar_bar_background');
        $sborder = \Drupal::config('sharebar.settings')
          ->get('sharebar_bar_border');
        if (\Drupal::config('sharebar.settings')
          ->get('sharebar_bar_oncontent')) {
          if (\Drupal::config('sharebar.settings')
            ->get('sharebar_bar_position') == 'left') {
            $mar = ' margin-right:' . \Drupal::config('sharebar.settings')
              ->get('sharebar_bar_rightoffset') . 'px;';
          }
          else {
            $mar = ' margin-left:' . \Drupal::config('sharebar.settings')
              ->get('sharebar_bar_leftoffset') . 'px;';
          }

          //$cls = 'sharebarp';
          $cls = \Drupal::config('sharebar.settings')
            ->get('sharebar_bar_idcontent');
          $sty = 'width: ' . \Drupal::config('sharebar.settings')
            ->get('sharebar_bar_swidth') . 'px; float: ' . \Drupal::config('sharebar.settings')
            ->get('sharebar_bar_position') . ';' . $mar;
        }
        else {

          //$cls = 'sharebar';
          $cls = \Drupal::config('sharebar.settings')
            ->get('sharebar_bar_id');
          $settings['cls'] = "#" . $cls;
          _drupal_add_js(array(
            'sharebar' => $settings,
          ), 'setting');
          $sty = '';
        }
        $str = '<ul id="' . $cls . '" style="background:#' . $sbg . ';border-color:#' . $sborder . ';margin-top:' . \Drupal::config('sharebar.settings')
          ->get('sharebar_bar_toptoffset') . 'px;' . $sty . '">';
        $buttons = unserialize(\Drupal::config('sharebar.settings')
          ->get('sharebar_buttons'));
        if (empty($buttons)) {
          $buttons = unserialize(sharebar_buttons_def());
        }
        if (is_array($buttons) && count($buttons)) {
          usort($buttons, "sharebar_cmp_up");
          foreach ($buttons as $key => $value) {
            if ($value->enabled) {
              $str .= '<li>' . sharebar_bar_filter($value->big_button, \Drupal::service("title_resolver")
                ->getTitle(\Drupal::request(), \Drupal::routeMatch()
                ->getRouteObject())) . '</li>';

              //print_r($str);

              //echo "here"; die;
            }
            if ($value->enabled && \Drupal::config('sharebar.settings')
              ->get('sharebar_bar_horizontal') && !\Drupal::config('sharebar.settings')
              ->get('sharebar_bar_oncontent')) {
              $strh .= '<li>' . sharebar_bar_filter($value->small_button, \Drupal::service("title_resolver")
                ->getTitle(\Drupal::request(), \Drupal::routeMatch()
                ->getRouteObject())) . '</li>';
            }
          }
        }
        $str .= '</ul>';
        if (\Drupal::config('sharebar.settings')
          ->get('sharebar_bar_horizontal')) {
          $hrcls = \Drupal::config('sharebar.settings')
            ->get('sharebar_bar_idhorizontal');
          $settings['hrcls'] = "#" . $hrcls;
          _drupal_add_js(array(
            'sharebar' => $settings,
          ), 'setting');
          $str .= '<ul id="' . $hrcls . '"' . $strh . '</ul>';
        }
        $variables['sharebar'] = $str;

        // . $variables['content'];
      }
    }
  }
}

/**
 * Function to calculate higher weight & move up.
 */
function sharebar_cmp_up($a, $b) {
  return $a->weight > $b->weight;
}

/**
 * Function to calculate lower weight & move down.
 */
function sharebar_cmp_down($a, $b) {
  return $a->weight < $b->weight;
}

/**
 * Replaces dynamic data with their values.
 */
function sharebar_bar_filter($input, $title = '') {
  $args = explode('/', current_path());
  $name = '';
  if ($args[0] == 'node' && is_numeric($args[1])) {
    $node = \Drupal::routeMatch()
      ->getParameter('node');
    if ($node->nid > 0) {
      $title = $node
        ->getTitle();
      $name = $node
        ->getOwner()
        ->getUsername();
    }
  }
  $path = isset($_GET['q']) ? $_GET['q'] : '<front>';
  $currenturl = \Drupal::URL($path, [], [
    'absolute' => TRUE,
  ]);
  $code = array(
    '[title]',
    '[url]',
    '[author]',
    '[twitter]',
  );
  $values = array(
    $title,
    $currenturl,
    $name,
    \Drupal::config('sharebar.settings')
      ->get('sharebar_bar_twitter_username'),
  );
  return str_replace($code, $values, $input);
}

/**
 * Function to defines default values.
 */
function sharebar_buttons_def() {
  return 'a:10:{s:4:"digg";O:8:"stdClass":6:{s:12:"machine_name";s:4:"digg";s:4:"name";s:4:"digg";s:10:"big_button";s:317:"<script type="text/javascript">(function() { var s = document.createElement(\'SCRIPT\\), s1 = document.getElementsByTagName(\'SCRIPT\')[0]; s.type = \'text/javascript\'; s.async = true; s.src = \'http://widgets.digg.com/buttons.js\'; s1.parentNode.insertBefore(s, s1); })(); </script><a class="DiggThisButton DiggMedium"></a>";s:12:"small_button";s:318:"<script type="text/javascript">(function() { var s = document.createElement(\'SCRIPT\'), s1 = document.getElementsByTagName(\'SCRIPT\')[0]; s.type = \'text/javascript\'; s.async = true; s.src = \'http://widgets.digg.com/buttons.js\'; s1.parentNode.insertBefore(s, s1); })(); </script><a class="DiggThisButton DiggCompact"></a>";s:7:"enabled";i:1;s:6:"weight";s:1:"2";}s:7:"twitter";O:8:"stdClass":6:{s:12:"machine_name";s:7:"twitter";s:4:"name";s:7:"twitter";s:10:"big_button";s:217:"<a href="http://twitter.com/share" class="twitter-share-button" data-url="[url]"data-count="vertical" data-via="[twitter]">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>";s:12:"small_button";s:220:"<a href="http://twitter.com/share" class="twitter-share-button" data-url="[url]" data-count="horizontal" data-via="[twitter]">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>";s:7:"enabled";i:1;s:6:"weight";s:1:"0";}s:8:"facebook";O:8:"stdClass":6:{s:12:"machine_name";s:8:"facebook";s:4:"name";s:8:"facebook";s:10:"big_button";s:447:"<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> <html xmlns:fb="http://ogp.me/ns/fb#"> <fb:like href="[url]" send="true" layout="box_count" width="450" show_faces="true"></fb:like>";s:12:"small_button";s:450:"<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> <html xmlns:fb="http://ogp.me/ns/fb#"> <fb:like href="[url]" send="true" layout="button_count" width="450" show_faces="true"></fb:like>";s:7:"enabled";i:1;s:6:"weight";s:1:"1";}s:6:"reddit";O:8:"stdClass":6:{s:12:"machine_name";s:6:"reddit";s:4:"name";s:6:"reddit";s:10:"big_button";s:89:"<script type="text/javascript" src="http://reddit.com/static/button/button2.js"></script>";s:12:"small_button";s:89:"<script type="text/javascript" src="http://reddit.com/static/button/button1.js"></script>";s:7:"enabled";i:1;s:6:"weight";s:1:"4";}s:5:"dzone";O:8:"stdClass":6:{s:12:"machine_name";s:5:"dzone";s:4:"name";s:5:"dzone";s:10:"big_button";s:94:"<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>";s:12:"small_button";s:94:"<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>";s:7:"enabled";i:1;s:6:"weight";s:1:"5";}s:11:"stumbleupon";O:8:"stdClass":6:{s:12:"machine_name";s:11:"stumbleupon";s:4:"name";s:11:"stumbleupon";s:10:"big_button";s:70:"<script src="http://www.stumbleupon.com/hostedbadge.php?s=5"></script>";s:12:"small_button";s:70:"<script src="http://www.stumbleupon.com/hostedbadge.php?s=2"></script>";s:7:"enabled";i:1;s:6:"weight";s:1:"3";}s:11:"designfloat";O:8:"stdClass":6:{s:12:"machine_name";s:11:"designfloat";s:4:"name";s:11:"designfloat";s:10:"big_button";s:150:"<script type="text/javascript">submit_url = \'[url]\';</script><script type="text/javascript" src="http://www.designfloat.com/evb2/button.php"></script>";s:12:"small_button";s:149:"<script type="text/javascript">submit_url = \'[url]\';</script><script type="text/javascript" src="http://www.designfloat.com/evb/button.php"></script>";s:7:"enabled";i:1;s:6:"weight";s:1:"6";}s:5:"email";O:8:"stdClass":6:{s:12:"machine_name";s:5:"email";s:4:"name";s:5:"email";s:10:"big_button";s:71:"<a href="mailto:?subject=[url]" class="sharebar-button email">Email</a>";s:12:"small_button";s:71:"<a href="mailto:?subject=[url]" class="sharebar-button email">Email</a>";s:7:"enabled";i:1;s:6:"weight";s:1:"7";}s:8:"linkedin";O:8:"stdClass":6:{s:12:"machine_name";s:8:"linkedin";s:4:"name";s:8:"linkedin";s:10:"big_button";s:158:"<script src="//platform.linkedin.com/in.js" type="text/javascript">
 lang: en_US</script><script type="IN/Share" data-url="[url]" data-counter="top"></script>";s:12:"small_button";s:160:"<script src="//platform.linkedin.com/in.js" type="text/javascript">
 lang: en_US</script><script type="IN/Share" data-url="[url]" data-counter="right"></script>";s:7:"enabled";i:1;s:6:"weight";s:3:"-50";}s:10:"googleplus";O:8:"stdClass":6:{s:12:"machine_name";s:10:"googleplus";s:4:"name";s:10:"googleplus";s:10:"big_button";s:462:"<!-- Place this tag where you want the +1 button to render. -->
<div class="g-plusone" data-size="tall" data-href="[url]"></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/plusone.js\';var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(po, s);})();</script>";s:12:"small_button";s:443:"<!-- Place this tag where you want the +1 button to render. --><div class="g-plusone" data-href="[url]"></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/plusone.js\';var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(po, s);})();</script>";s:7:"enabled";i:1;s:6:"weight";s:3:"-50";}}';
}

/**
 * Theme the administer buttons page.
 *
 * @ingroup themeable
 */
function template_preprocess_sharebar_buttons_table(&$variables) {
  $add_url = Url::fromRoute('sharebar.add_button');
  $variables['add_button'] = \Drupal::l(t('Add New Button'), $add_url);
  $buttons = unserialize(\Drupal::config('sharebar.settings')
    ->get('sharebar_buttons'));
  if (empty($buttons)) {
    $buttons = unserialize(sharebar_buttons_def());
  }
  if (is_array($buttons) && count($buttons)) {
    usort($buttons, "sharebar_cmp_up");
    foreach ($buttons as $key => $value) {
      $row = array();
      $row[] = array(
        'data' => $value->enabled ? t('Yes') : t('No'),
      );
      $row[] = array(
        'data' => $value->name,
      );
      $row[] = array(
        'data' => $value->big_button,
      );
      $row[] = array(
        'data' => $value->small_button,
      );
      $row[] = array(
        'data' => $value->weight,
      );
      $edit_url = Url::fromRoute('sharebar.edit_button', array(
        'mname' => $value->name,
      ));
      $del_url = Url::fromRoute('sharebar.del_button', array(
        'mname' => $value->name,
      ));
      $edit_url1 = \Drupal::l(t('Edit'), $edit_url);
      $del_url1 = \Drupal::l(t('Delete'), $del_url);
      $row[] = array(
        'data' => $edit_url1,
      );
      $row[] = array(
        'data' => $del_url1,
      );
      $rows[] = $row;
    }
  }
  if (count($rows)) {
    $header = array(
      t('Enabled'),
      t('Name'),
      t('Big Button'),
      t('Small Button'),
      t('Weight'),
      t('Edit'),
      t('Delete'),
    );
    $table = array(
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#attributes' => array(
        'id' => 'sharebar-table',
      ),
    );
    $variables['out'] = drupal_render($table);
  }
  else {
    $variables['out'] = '<b>' . t('No data') . '</b>';
  }
}

Functions

Namesort descending Description
sharebar_bar_filter Replaces dynamic data with their values.
sharebar_buttons_def Function to defines default values.
sharebar_cmp_down Function to calculate lower weight & move down.
sharebar_cmp_up Function to calculate higher weight & move up.
sharebar_preprocess_page Implements hook_preprocess_page().
sharebar_preprocess_region Preprocess variables for region.tpl.php
sharebar_theme Implements hook_theme().
template_preprocess_sharebar_buttons_table Theme the administer buttons page.

Constants