You are here

socbutt.module in Bootstrap Social Sharing Buttons 8

Same filename and directory in other branches
  1. 7 socbutt.module

Allows integration with the Twitter microblogging service.

File

socbutt.module
View source
<?php

/**
 * @file
 * Allows integration with the Twitter microblogging service.
 */
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Url;

// Module constants.
define('SOCBUTT_LAYOUT_VERTICAL', 0);
define('SOCBUTT_LAYOUT_HORIZONTAL', 1);

/**
 * Implements hook_token_info().
 */
function socbutt_token_info() {
  $type = array(
    'name' => t('Socbutt tokens'),
    'description' => t("Socbutt tokens."),
  );
  $info['vertical'] = array(
    'name' => t("Social button bar, vertical"),
    'description' => t("Social button bar, vertical layout."),
    'type' => 'socbutt',
  );
  $info['horizontal'] = array(
    'name' => t("Social button bar, horizontal"),
    'description' => t("Social button bar, horizontal layout."),
    'type' => 'socbutt',
  );
  return array(
    'types' => array(
      'socbutt' => $type,
    ),
    'tokens' => array(
      'socbutt' => $info,
    ),
  );
}

/**
 * Implements hook_tokens().
 */
function socbutt_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();
  if ($type == 'socbutt') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'vertical':
          $replacements[$original] = \Drupal::service('renderer')
            ->render(_socbutt_buttons_render(array(
            'layout' => SOCBUTT_LAYOUT_VERTICAL,
          )));
          break;
        case 'horizontal':
          $replacements[$original] = \Drupal::service('renderer')
            ->render(_socbutt_buttons_render(array(
            'layout' => SOCBUTT_LAYOUT_HORIZONTAL,
          )));
          break;
      }
    }
  }
  return $replacements;
}

/**
 * Implements hook_theme().
 */
function socbutt_theme() {
  return array(
    'socbutt-buttons' => array(
      'variables' => array(
        'layout' => SOCBUTT_LAYOUT_VERTICAL,
        'title' => NULL,
        'body' => NULL,
        'url' => NULL,
      ),
    ),
  );
}

/**
 * Theme function to render social buttons.
 */
function _socbutt_buttons_render($variables) {
  global $base_url;
  $horizontal = $variables['layout'] == SOCBUTT_LAYOUT_HORIZONTAL;
  $tag = $horizontal ? 'span' : 'div';
  $horizontal_class = $horizontal ? ' inline' : '';
  $current_url = $base_url . \Drupal::service('path.current')
    ->getPath();

  // Preparing default sharing data.
  $request = \Drupal::request();
  $route_match = \Drupal::routeMatch();
  $title = \Drupal::service('title_resolver')
    ->getTitle($request, $route_match
    ->getRouteObject());
  $share_data = array(
    'title' => !is_null($variables['title']) ? $variables['title'] : $title,
    'body' => !is_null($variables['body']) ? $variables['body'] : '',
    'url' => !is_null($variables['url']) ? $variables['url'] : $current_url,
  );

  // URL encode so the strings can be used inside URLs.
  foreach ($share_data as $key => $data) {
    $share_data[$key] = rawurlencode($data);
  }
  $title = $share_data['title'];
  $body = $share_data['body'];
  $url = $share_data['url'];
  $btn_classes = array(
    'btn',
    'btn-info',
    'btn-xs',
  );
  if (!$horizontal) {
    $btn_classes[] = 'btn-block';
  }

  // Prepare render array.
  $render_array = array(
    '#prefix' => '<div class="social-share-links' . $horizontal_class . '">',
    '#suffix' => '</div>',
    '#attached' => array(
      'library' => array(
        'socbutt/socbutt',
      ),
    ),
  );

  // Link for email.
  $link = "mailto:?subject={$title}&body={$body}";
  $text = t('Email');
  $url_obj = Url::fromUri($link);
  $external_link = \Drupal::getContainer()
    ->get('link_generator')
    ->generate($text, $url_obj, array(
    'html' => TRUE,
    'attributes' => array(
      'class' => $btn_classes,
    ),
  ));
  $render_array['email'] = array(
    '#prefix' => '<' . $tag . ' class="share-link share-email">',
    '#suffix' => "</{$tag}>",
    '#markup' => $external_link,
  );

  // Facebook share.
  $link = "https://www.facebook.com/sharer/sharer.php?u={$url}";
  $text = t('Facebook');
  $url_obj = Url::fromUri($link);
  $external_link = \Drupal::getContainer()
    ->get('link_generator')
    ->generate($text, $url_obj, array(
    'html' => TRUE,
    'attributes' => array(
      'target' => '_blank',
      'class' => $btn_classes,
    ),
  ));
  $render_array['facebook'] = array(
    '#prefix' => '<' . $tag . ' class="share-link share-facebook">',
    '#suffix' => "</{$tag}>",
    '#markup' => $external_link,
  );

  // Twitter share.
  // Twitter adds the $url after this automatically.
  $twitter_msg = $title . ' -';
  $link = "https://twitter.com/intent/tweet?text={$twitter_msg}&url={$url}";
  $text = t('Twitter');
  $url_obj = Url::fromUri($link);
  $external_link = \Drupal::getContainer()
    ->get('link_generator')
    ->generate($text, $url_obj, array(
    'html' => TRUE,
    'attributes' => array(
      'target' => '_blank',
      'class' => $btn_classes,
    ),
  ));
  $render_array['twitter'] = array(
    '#prefix' => '<' . $tag . ' class="share-link share-twitter">',
    '#suffix' => "</{$tag}>",
    '#markup' => $external_link,
  );

  // Google Plus share:
  $link = "https://plus.google.com/share?url={$url}";
  $text = t('Google Plus');
  $url_obj = Url::fromUri($link);
  $external_link = \Drupal::getContainer()
    ->get('link_generator')
    ->generate($text, $url_obj, array(
    'html' => TRUE,
    'attributes' => array(
      'target' => '_blank',
      'class' => $btn_classes,
    ),
  ));
  $render_array['gplus'] = array(
    '#prefix' => '<' . $tag . ' class="share-link share-gplus">',
    '#suffix' => "</{$tag}>",
    '#markup' => $external_link,
  );

  // LinkedIn share.
  $link = "https://www.linkedin.com/shareArticle?mini=true&url={$url}&title={$title}&summary={$body}&source={$url}";
  $text = t('LinkedIn');
  $url_obj = Url::fromUri($link);
  $external_link = \Drupal::getContainer()
    ->get('link_generator')
    ->generate($text, $url_obj, array(
    'html' => TRUE,
    'attributes' => array(
      'target' => '_blank',
      'class' => $btn_classes,
    ),
  ));
  $render_array['linkedin'] = array(
    '#prefix' => '<' . $tag . ' class="share-link share-linkedin">',
    '#suffix' => "</{$tag}>",
    '#markup' => $external_link,
  );

  // Render.
  return $render_array;
}

Functions

Namesort descending Description
socbutt_theme Implements hook_theme().
socbutt_tokens Implements hook_tokens().
socbutt_token_info Implements hook_token_info().
_socbutt_buttons_render Theme function to render social buttons.

Constants