You are here

responsive_share_buttons.module in Responsive Share Buttons 7

Same filename and directory in other branches
  1. 8 responsive_share_buttons.module

Module to enable CSS responsive share buttons.

File

responsive_share_buttons.module
View source
<?php

/**
 * @file
 * Module to enable CSS responsive share buttons.
 */

/**
 * Implements hook_help().
 */
function responsive_share_buttons_help($path, $arg) {
  $output = '';
  switch ($path) {
    case 'admin/config/modules#description':
      $output .= t('Add a responsive social media share block to all pages.');
      break;
    case 'admin/config/user-interface/responsive_share_buttons':
      $output .= t('<p>This is a simple module that adds a social media block to all pages.</p>');
      break;
  }
  return $output;
}

/**
 * Implements hook_menu().
 */
function responsive_share_buttons_menu() {
  $items['admin/config/user-interface/responsive_share_buttons'] = array(
    'title' => 'Responsive Share Buttons',
    'description' => 'Configure Responsive Share Buttons.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'responsive_share_buttons_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'responsive_share_buttons.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function responsive_share_buttons_permission() {
  return array(
    'configure responsive share buttons' => array(
      'title' => t('Configure responsive share buttons'),
    ),
  );
}

/**
 * Implements hook_block_info().
 */
function responsive_share_buttons_block_info() {
  $blocks = array();
  $blocks['share'] = array(
    'info' => t('Responsive Share Buttons'),
    'cache' => DRUPAL_CACHE_PER_PAGE,
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function responsive_share_buttons_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'share':
      $block['subject'] = '';
      $block['content'] = array(
        '#markup' => theme('responsive_share_buttons'),
        '#attached' => array(
          'css' => array(
            drupal_get_path('module', 'responsive_share_buttons') . '/css/responsive_share_buttons.css',
          ),
          'js' => array(
            drupal_get_path('module', 'responsive_share_buttons') . '/js/responsive_share_buttons.js',
          ),
        ),
      );
      break;
  }
  return $block;
}

/**
 * Implements hook_contextual_links_view_alter().
 */
function responsive_share_buttons_contextual_links_view_alter(&$element, $items) {
  $block = isset($element['#element']['#block']) ? $element['#element']['#block'] : NULL;
  if (is_object($block) && $block->module == 'responsive_share_buttons') {
    if ($block->delta == 'share' && user_access('configure responsive share buttons')) {
      $element['#links']['responsive_share_buttons'] = array(
        'title' => t('Configure responsive share buttons'),
        'href' => 'admin/config/user-interface/responsive_share_buttons',
        'query' => drupal_get_destination(),
        'attributes' => array(
          'title' => t('Configure the responsive share buttons links.'),
        ),
      );
    }
  }
}

/**
 * Implements hook_theme().
 */
function responsive_share_buttons_theme($existing, $type, $theme, $path) {
  return array(
    'responsive_share_buttons' => array(
      'template' => 'responsive_share_buttons',
      'variables' => array(
        'buttons' => NULL,
      ),
    ),
    'responsive_share_buttons_settings' => array(
      'render element' => 'form',
    ),
  );
}

/**
 * Theme function for slideshow_manage().
 */
function theme_responsive_share_buttons_settings($variables) {
  $form = $variables['form'];
  $rows = array();
  foreach (element_children($form['networks']) as $network) {
    $form['networks'][$network]['weight']['#attributes']['class'] = array(
      'share-networks-order-weight',
    );
    $rows[] = array(
      'data' => array(
        array(
          'class' => array(
            'slide-cross',
          ),
        ),
        drupal_render($form['networks'][$network]['active']),
        drupal_render($form['networks'][$network]['weight']),
      ),
      'class' => array(
        'draggable',
      ),
    );
  }
  $header = array(
    t('Order'),
    t('Active'),
    t('Weight'),
  );
  $output = theme('table', array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => array(
      'id' => 'share-networks-order',
    ),
  ));
  $output .= drupal_render_children($form);
  drupal_add_tabledrag('share-networks-order', 'order', 'sibling', 'share-networks-order-weight');
  return $output;
}

/**
 * Theme preprocess function for theme_responsive_share_buttons().
 */
function template_preprocess_responsive_share_buttons(&$vars) {
  $networks = variable_get('responsive_share_buttons', NULL);
  $url = urlencode(url(current_path(), array(
    'absolute' => TRUE,
  )));
  $title = urlencode(drupal_get_title());

  // Sort the network order by weight.
  uasort($networks, 'drupal_sort_weight');
  foreach ($networks as $name => $network) {
    if (!empty($network['active'])) {
      $links[] = _prepare_share_link($name, $url, $title);
    }
  }
  drupal_alter('responsive_share_buttons_links', $links);
  $vars['buttons'] = theme('item_list', array(
    'items' => $links,
    'attributes' => array(
      'class' => array(
        'share-inner-wrp',
      ),
    ),
  ));
}

/**
 * Prepare a sharing link.
 *
 * @param string $network
 *   The name of the social network to use.
 *
 * @param string $url
 *   The URL to share.
 *
 * @param string $title
 *   The title to use.
 *
 * @return string
 *   The link for sharing.
 */
function _prepare_share_link($network, $url, $title) {
  $link = '';
  $link_options = array(
    'attributes' => array(
      'class' => array(
        'button-wrap',
        $network,
      ),
    ),
  );
  switch ($network) {
    case 'delicious':
      $link_text = t('Delicious');
      $link_url = 'http://del.icio.us/post?url=' . $url . '&amp;title=' . $title;
      break;
    case 'digg':
      $link_text = t('Digg it');
      $link_url = 'http://www.digg.com/submit?phase=2&amp;url=' . $url . '&amp;title=' . $title;
      break;
    case 'facebook':
      $link_text = t('Facebook');
      $link_url = 'https://www.facebook.com/sharer/sharer.php?u=' . $url . '&amp;title=' . $title;
      break;
    case 'google':
      $link_text = t('Plus Share');
      $link_url = 'https://plus.google.com/share?url=' . $url . '&amp;title=' . $title;
      break;
    case 'linkedin':
      $link_text = t('LinkedIn');
      $link_url = 'http://www.linkedin.com/shareArticle?mini=true&url=' . $url . '&amp;title=' . $title;
      break;
    case 'pinterest':
      $link_text = t('Pinterest');
      $link_url = 'https://www.pinterest.com/pin/create/button/?url=' . $url . '&description=' . $title;
      break;
    case 'stumbleupon':
      $link_text = t('Stumbleupon');
      $link_url = 'http://www.stumbleupon.com/submit?url=' . $url . '&amp;title=' . $title;
      break;
    case 'twitter':
      if ($twitter_name = variable_get('responsive_share_buttons_twitter_name', '')) {
        $title .= t(' via @@twitter_name', array(
          '@twitter_name' => $twitter_name,
        ));
      }
      $link_text = t('Tweet');
      $link_url = 'http://twitter.com/home?status=' . $title . ' ' . $url;
      break;
  }
  drupal_alter('responsive_share_buttons_share_link', $link_text, $link_url, $link_options, $network);
  if (!empty($link_text)) {
    $link = l($link_text, $link_url, $link_options, $network);
  }
  return $link;
}