You are here

tweetbutton.module in Tweet Button 8

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

Adds block with tweet and follow buttons.

File

tweetbutton.module
View source
<?php

/**
 * @file
 * Adds block with tweet and follow buttons.
 */
use Drupal\Core\Language\Language;

/**
 * Implements hook_help().
 */
function tweetbutton_help($path, $arg) {
  switch ($path) {
    case 'admin/help#tweetbutton':
    case 'admin/config/services/tweetbutton':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output = '<p>' . t('This button allows your website to let people share content on Twitter without having to leave the page. Promote strategic Twitter accounts at the same time while driving traffic to your website. These are the general configuration options for the button.') . '</p>';
      $output .= '<p>' . t('To add tweetbutton you have to create a tweetbutton field and add it to your content (node, taxonomy, user).') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_theme()
 */
function tweetbutton_theme() {
  return array(
    'tweetbutton_tweet_display' => array(
      'variables' => array(
        'options' => array(),
      ),
    ),
    'tweetbutton_follow_display' => array(
      'variables' => array(
        'options' => array(),
      ),
    ),
    'tweetbutton_hashtag_display' => array(
      'variables' => array(
        'options' => array(),
        'hashtag' => '',
      ),
    ),
  );
}

/**
 * Implementation of hook_permission()
 */
function tweetbutton_permission() {
  return array(
    'administer tweetbutton' => array(
      'title' => t('Administer Tweet Button'),
    ),
    'access tweetbutton' => array(
      'title' => t('Access Tweet Button'),
    ),
    'use tweetbutton field' => array(
      'title' => t('Use tweetbutton field to alter tweet text'),
    ),
  );
}

/**
 * Implementation of hook_menu()
 */
function tweetbutton_menu() {
  $items = array();
  $items['admin/config/services/tweetbutton'] = array(
    'title' => 'Tweet Button',
    'description' => 'Configure the look and behavior of the Tweet Button appears on the site.',
    'route_name' => 'tweetbutton.settings',
  );
  return $items;
}

/**
 * Helper function to build the required tweet attributes.
 *
 * @param array $options
 *   Context specific options.
 *
 * @return
 *   List of attributes to be rendered for tweet button.
 */
function tweetbutton_tweet_get_attributes($options = array()) {
  $config = \Drupal::config('tweetbutton.settings');
  $default_option = array(
    'account' => $config
      ->get('tweetbutton_account'),
    'text' => $config
      ->get('tweetbutton_tweet_text'),
    'rel_account' => $config
      ->get('tweetbutton_rel_account_name'),
    'rel_description' => $config
      ->get('tweetbutton_rel_account_description'),
    'language' => $config
      ->get('tweetbutton_language'),
    'hashtags' => $config
      ->get('tweetbutton_hashtags'),
  );

  // @TODO: Integration with module shorten.
  $default_option['url'] = '';
  $default_option['count_url'] = '';
  $options += $default_option;
  $attributes = array(
    'data-url' => $options['url'],
    'data-via' => $options['account'],
    'data-text' => $options['text'],
    'data-related' => $options['rel_account'] . ':' . $options['rel_description'],
    'data-count' => $options['count'],
    'data-lang' => $options['language'],
    'data-counturl' => $options['count_url'],
    'data-hashtags' => $options['hashtags'],
    'data-size' => $options['size'],
    'data-dnt' => $options['dnt'] ? 'true' : 'false',
    'class' => 'twitter-share-button',
  );
  return $attributes;
}

/**
 * Helper function to build attributes of follow button.
 *
 * @param array $options
 *   Context specific options.
 *
 * @return
 *   List of attributes to be rendered for follow button.
 */
function tweetbutton_follow_get_attributes($options) {
  $language = \Drupal::languageManager()
    ->getCurrentLanguage();
  $attributes = array(
    'data-show-count' => $options['show_count'] ? 'true' : 'false',
    'data-lang' => $options['lang'] == 'auto' ? $language->id : $options['lang'],
    'data-width' => $options['width'] . 'px',
    'data-show-screen-name' => $options['show_screen_name'] ? 'true' : 'false',
    'data-size' => $options['size'],
    'data-dnt' => $options['dnt'] ? 'true' : 'false',
    'class' => 'twitter-follow-button',
  );
  if ($options['align'] !== 'none') {
    $attributes['data-align'] = $options['align'];
  }
  return $attributes;
}

/**
 * Helper function to build attributes of hashtag.
 *
 * @param array $options
 *   Context specific options.
 *
 * @return
 *   List of attributes to be rendered for hashtag.
 */
function tweetbutton_hashtag_get_attributes($options) {
  $language = \Drupal::languageManager()
    ->getCurrentLanguage();
  $default_options = array(
    'rel_account' => variable_get('tweetbutton_account'),
  );
  $options += $default_options;
  $attributes = array(
    'data-related' => $options['rel_account'],
    'data-lang' => $options['language'] == 'auto' ? $language->id : $options['language'],
    'class' => 'twitter-hashtag-button',
  );
}

/**
 * Returns HTML for a tweet button.
 *
 * @param array $variables
 *   An associative array with options of tweet button.
 */
function theme_tweetbutton_tweet_display($variables) {
  $script_url = url('http://platform.twitter.com/widgets.js', array(
    'external' => TRUE,
    'https' => TRUE,
  ));
  $options = $variables['options'];
  $attributes = tweetbutton_tweet_get_attributes($options);
  $tweetbutton_label = isset($options['tweetbutton_label']) ? $options['tweetbutton_label'] : t('Tweet');
  $tweet_link = l($tweetbutton_label, 'http://twitter.com/share', array(
    'attributes' => $attributes,
    'external' => TRUE,
    'https' => TRUE,
  ));
  $link = array(
    '#markup' => $tweet_link,
    '#prefix' => '<div class="tweetbutton-tweet tweetbutton">',
    '#suffix' => '</div>',
    '#attached' => array(
      'js' => array(
        $script_url => array(
          'type' => 'external',
          'scope' => 'footer',
        ),
      ),
    ),
  );
  return drupal_render($link);
}

/**
 * Returns HTML for a follow button.
 *
 * @param array $variables
 *   An associative array with options of follow button.
 */
function theme_tweetbutton_follow_display($variables) {
  $script_url = url('http://platform.twitter.com/widgets.js', array(
    'external' => TRUE,
    'https' => TRUE,
  ));
  $options = $variables['options'];
  $screen_name = !empty($options['screen_name']) ? $options['screen_name'] : \Drupal::config('tweetbutton.settings')
    ->get('tweetbutton_follow_screen_name');
  $follow_text = t('Follow !screen_name', array(
    '!screen_name' => $screen_name,
  ));
  $twitter_account_link = 'http://twitter.com/' . $screen_name;
  $attributes = tweetbutton_follow_get_attributes($options);
  $follow_link = l($follow_text, $twitter_account_link, array(
    'attributes' => $attributes,
    'external' => TRUE,
    'https' => TRUE,
  ));
  $link = array(
    '#markup' => $follow_link,
    '#prefix' => '<div class="tweetbutton-follow tweetbutton">',
    '#suffix' => '</div>',
    '#attached' => array(
      'js' => array(
        $script_url => array(
          'type' => 'external',
          'scope' => 'footer',
        ),
      ),
    ),
  );
  return drupal_render($link);
}

/**
 * Returns HTML for a hashtag.
 *
 * @param array $variables
 *   An associative array with options of hashtag.
 */
function theme_tweetbutton_hashtag_display($variables) {
  $script_url = url('http://platform.twitter.com/widgets.js', array(
    'external' => TRUE,
  ));
  $options = $variables['options'];
  $attributes = tweetbutton_hashtag_get_attributes($options);
  $query = array();
  if (!empty($options['tweet_text'])) {
    $query['tweet_text'] = $options['tweet_text'];
  }
  if (!empty($variables['hashtag'])) {
    $query['button_hashtag'] = $variables['hashtag'];
  }
  $hash_link = l($hash_text, 'http://twitter.com/intent/tweet', array(
    'query' => $query,
    'attributes' => $attributes,
    'external' => TRUE,
    'https' => TRUE,
  ));
  $hash_link = array(
    '#markup' => $hash_link,
    '#prefix' => '<div class="tweetbutton-hashtag tweetbutton">',
    '#suffix' => '</div>',
    '#attached' => array(
      'js' => array(
        $script_url => array(
          'type' => 'external',
          'scope' => 'footer',
        ),
      ),
    ),
  );
  return drupal_render($hash_link);
}

Functions

Namesort descending Description
theme_tweetbutton_follow_display Returns HTML for a follow button.
theme_tweetbutton_hashtag_display Returns HTML for a hashtag.
theme_tweetbutton_tweet_display Returns HTML for a tweet button.
tweetbutton_follow_get_attributes Helper function to build attributes of follow button.
tweetbutton_hashtag_get_attributes Helper function to build attributes of hashtag.
tweetbutton_help Implements hook_help().
tweetbutton_menu Implementation of hook_menu()
tweetbutton_permission Implementation of hook_permission()
tweetbutton_theme Implements hook_theme()
tweetbutton_tweet_get_attributes Helper function to build the required tweet attributes.