You are here

tweetbutton.module in Tweet Button 7

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

File

tweetbutton.module
View source
<?php

/**
 * Implementation of hook_help()
 */
function tweetbutton_help($path, $arg) {
  $output = NULL;
  switch ($path) {
    case 'admin/help#tweetbutton':
      $output = '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.';
      break;
    case 'admin/config/services/tweetbutton':
      $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. Make sure to check the Node Settings to set where the button will appear.') . '</p>';
      break;
    case 'admin/config/services/tweetbutton/node':
      $output = '<p>' . t('Select the node types and location on which the Tweet Button will appear.') . '</p>';
      break;
  }
  return $output;
}

/**
 * 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.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'tweetbutton_admin_settings',
    ),
    'access arguments' => array(
      'administer tweetbutton',
    ),
    'file' => 'tweetbutton.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/config/services/tweetbutton/settings'] = array(
    'title' => 'General Settings',
    'description' => 'Change the look and behavior of the Tweet Button.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'tweetbutton_admin_settings',
    ),
    'access arguments' => array(
      'administer tweetbutton',
    ),
    'file' => 'tweetbutton.admin.inc',
    'weight' => 0,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/config/services/tweetbutton/node'] = array(
    'title' => 'Node Settings',
    'description' => 'Configure how the Tweet Button interacts with nodes.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'tweetbutton_node_settings',
    ),
    'access arguments' => array(
      'administer tweetbutton',
    ),
    'file' => 'tweetbutton.admin.inc',
    'weight' => 1,
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implementation of hook_theme()
 */
function tweetbutton_theme() {
  return array(
    'tweetbutton_display' => array(
      'variables' => array(
        'object' => NULL,
        'options' => array(),
      ),
    ),
    'tweetbutton_follow_display' => array(
      'variables' => array(
        'options' => array(),
        'screen_name' => '',
      ),
    ),
  );
}

/**
 * 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'),
    ),
  );
}

/**
 * Implementation of hook_node_view()
 */
function tweetbutton_node_view($node, $view_mode) {
  $available_types = variable_get('tweetbutton_node_types', array(
    'article',
  ));
  $available_view_modes = variable_get('tweetbutton_node_location', array(
    'full',
  ));
  if (!empty($available_types[$node->type]) && user_access('access tweetbutton')) {
    if (!empty($available_view_modes[$view_mode])) {
      if ($view_mode != 'rss') {
        $node->content['tweetbutton'] = array(
          '#object' => $node,
          '#weight' => variable_get('tweetbutton_node_weight', -5),
          '#theme' => 'tweetbutton_display__' . $node->type,
        );
      }
      else {
        $query = array();
        $query['text'] = token_replace(variable_get('tweetbutton_tweet_text'), array(
          'node' => $node,
        ));
        if (empty($query['text'])) {
          $query['text'] = truncate_utf8($node->title, 120, TRUE, TRUE);
        }
        $query['via'] = variable_get('tweetbutton_account');
        $query['related'] = variable_get('tweetbutton_rel_account_name');

        // set url
        $uri = entity_uri('node', $node);
        $options['url'] = url($uri['path'], array(
          'absolute' => TRUE,
        ));
        $query['url'] = $options['url'];
        $language = variable_get('tweetbutton_language');
        $query['language'] = $language == 'auto' ? $GLOBALS['language']->language : $language;
        $node->content['tweetbutton']['#markup'] = l(t('Tweet'), 'http://twitter.com/intent/tweet', array(
          'query' => $query,
          'attributes' => array(
            'target' => '_blank',
          ),
        ));
      }
    }
    if (!empty($available_view_modes['links']) && $view_mode != 'rss') {
      $node->content['links']['#links']['node_tweetbutton_link'] = array(
        'title' => theme('tweetbutton_display__' . $node->type, array(
          'object' => $node,
          'options' => array(
            'type' => 'horizontal',
          ),
        )),
        'html' => TRUE,
      );
    }
  }
}

/**
 * Implementation of hook_block_info()
 */
function tweetbutton_block_info() {
  $blocks['tweetbutton'] = array(
    'info' => t('Tweetbutton tweet'),
  );
  $blocks['tweetbutton_follow'] = array(
    'info' => t('Tweetbutton follow'),
  );
  return $blocks;
}

/**
 * Implementation of hook_block_configure
 */
function tweetbutton_block_view($delta = '') {
  if ($delta == 'tweetbutton') {
    $block['subject'] = t('Tweetbutton tweet');
    $block['content'] = theme('tweetbutton_display');
    return $block;
  }
  elseif ($delta == 'tweetbutton_follow') {
    $block['subject'] = t('Tweetbutton follow');
    $block['content'] = theme('tweetbutton_follow_display');
    return $block;
  }
}

/**
 * Helper function to build the required tweet attributes
 * @param $object
 *   Entity object can either be (node, user, taxonomy) 
 * @param $options
 *   Context specific options
 * @return 
 *   List of attributes to be rendered for twitter tweetbutton
 */
function tweetbutton_get_attributes($object = NULL, $options = array()) {
  global $language;
  $entity_type = empty($options['entity_type']) ? 'node' : $options['entity_type'];
  $default_option = array(
    'type' => variable_get('tweetbutton_button', 'vertical'),
    'text' => variable_get('tweetbutton_tweet_text'),
    'language' => variable_get('tweetbutton_language'),
    'account' => variable_get('tweetbutton_account'),
    'rel_account' => variable_get('tweetbutton_rel_account_name'),
    'rel_desc' => variable_get('tweetbutton_rel_account_description'),
  );
  if (empty($options['url'])) {
    if (empty($object)) {
      $options['url'] = url($_GET['q'], array(
        'absolute' => TRUE,
      ));
    }
    else {
      $uri = entity_uri($entity_type, $object);
      $options['url'] = url($uri['path'], array(
        'absolute' => TRUE,
      ));
    }
  }
  $options += $default_option;
  if ($entity_type == 'taxonomy_term') {
    $token_type = 'term';
  }
  else {
    $token_type = $entity_type;
  }
  $attributes = array(
    'data-count' => $options['type'],
    'data-via' => $options['account'],
    'data-related' => $options['rel_account'] . ':' . $options['rel_desc'],
    'data-text' => !empty($options['text']) ? token_replace($options['text'], array(
      $token_type => $object,
    )) : '',
    'data-counturl' => $options['url'],
    'data-url' => $options['url'],
    'data-lang' => $options['language'] == 'auto' ? $language->language : $options['language'],
    'class' => 'twitter-share-button',
  );
  return $attributes;
}
function theme_tweetbutton_display($variables) {
  $script_url = url('http://platform.twitter.com/widgets.js', array(
    'external' => TRUE,
  ));
  drupal_add_js($script_url, array(
    'type' => 'external',
    'scope' => 'footer',
  ));
  $object = $variables['object'];
  $options = $variables['options'];
  $attributes = tweetbutton_get_attributes($object, $options);
  $tweet_link = l(t('Tweet'), 'http://twitter.com/share', array(
    'attributes' => $attributes,
    'external' => TRUE,
  ));
  $link = '<div class="tweetbutton">' . $tweet_link . '</div>';
  return $link;
}
function tweetbutton_follow_get_attributes($options) {
  global $language;
  $default_options = array(
    'show_count' => variable_get('tweetbutton_follow_show_count', TRUE),
    'show_screen_name' => variable_get('tweetbutton_follow_screen_name'),
    'size' => variable_get('tweetbutton_follow_size'),
    'language' => variable_get('tweetbutton_language'),
  );
  $options += $default_options;
  $attributes = array(
    'data-show-count' => $options['show_count'] ? 'true' : 'false',
    'data-show-screen-name' => $options['show_screen_name'],
    'data-size' => $options['size'],
    'data-lang' => $options['language'] == 'auto' ? $language->language : $options['language'],
    'class' => 'twitter-follow-button',
  );
  return $attributes;
}
function theme_tweetbutton_follow_display($variables) {
  $script_url = url('http://platform.twitter.com/widgets.js', array(
    'external' => TRUE,
    'https' => TRUE,
  ));
  drupal_add_js($script_url, array(
    'type' => 'external',
    'scope' => 'footer',
  ));
  $options = $variables['options'];
  $screen_name = $variables['screen_name'];
  if (empty($screen_name)) {
    $screen_name = variable_get('tweetbutton_follow_screen_name');
  }
  $follow_user_text = isset($options['tweetbutton_label']) ? $options['tweetbutton_label'] : 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_user_text, $twitter_account_link, array(
    'attributes' => $attributes,
    'external' => TRUE,
    'https' => TRUE,
  ));
  $link = '<div class="tweetbutton-follow tweetbutton">' . $follow_link . '</div>';
  return $link;
}

/**
 *  Implementation of hook_views_api().
 */
function tweetbutton_views_api() {
  return array(
    'api' => '2.0',
  );
}

Functions

Namesort descending Description
theme_tweetbutton_display
theme_tweetbutton_follow_display
tweetbutton_block_info Implementation of hook_block_info()
tweetbutton_block_view Implementation of hook_block_configure
tweetbutton_follow_get_attributes
tweetbutton_get_attributes Helper function to build the required tweet attributes
tweetbutton_help Implementation of hook_help()
tweetbutton_menu Implementation of hook_menu()
tweetbutton_node_view Implementation of hook_node_view()
tweetbutton_permission Implementation of hook_permission()
tweetbutton_theme Implementation of hook_theme()
tweetbutton_views_api Implementation of hook_views_api().