You are here

tweetbutton.module in Tweet Button 6

Same filename and directory in other branches
  1. 8 tweetbutton.module
  2. 7.2 tweetbutton.module
  3. 7 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':
      return 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.');
  }
}

/**
 * Implementation of hook_menu()
 */
function tweetbutton_menu() {
  $items = array();
  $items['admin/settings/tweetbutton/list'] = array(
    'title' => 'Tweetbutton Settings',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -1,
  );
  $items['admin/settings/tweetbutton'] = array(
    'title' => 'Tweetbutton Settings',
    'description' => 'Configure tweetbutton settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'tweetbutton_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'tweetbutton.admin.inc',
    'weight' => 0,
  );
  $items['admin/settings/tweetbutton/node'] = array(
    'title' => 'Tweetbutton Node Settings',
    'description' => 'Configure tweetbutton for nodes',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'tweetbutton_node_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    '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(
        'entity' => NULL,
        'options' => NULL,
      ),
    ),
  );
}

/**
 * Implementation of hook_permission()
 */
function tweetbutton_perm() {
  return array(
    'access tweetbutton',
  );
}

/**
 * Implementation of hook_nodeapi()
 */
function tweetbutton_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($a3) {
    $node_location = 'teaser';
  }
  else {
    $node_location = 'full';
  }
  $locations = variable_get('tweetbutton_node_location', array(
    'full',
  ));

  // Do not display tweetbutton in node content if its set in node links
  if (in_array($node->type, variable_get('tweetbutton_node_types', array(
    'story',
  )), TRUE) && !empty($locations[$node_location]) && user_access('access tweetbutton') && tweetbutton_allowed_path() && $op == 'view') {
    $weight = module_exists('content') ? content_extra_field_weight($node->type, 'tweetbutton_widget') : variable_get('tweetbutton_node_weight', -5);
    $node->content['tweetbutton'] = array(
      '#value' => theme('tweetbutton_display', $node),
      '#weight' => $weight,
    );
  }
}

/**
 * Implementation of hook_link
 */
function tweetbutton_link($entity_type, $entity, $teaser = FALSE) {
  $location = variable_get('tweetbutton_node_location', array(
    'full',
  ));
  if ($entity_type != 'node' || !user_access('access tweetbutton') || empty($location['links']) || !tweetbutton_allowed_path() || !in_array($entity->type, variable_get('tweetbutton_node_types', array(
    'story',
  )), TRUE)) {
    return;
  }
  $links = array();
  $links['tweetbutton'] = array(
    'title' => theme('tweetbutton_display', $entity, array(
      'type' => 'horizontal',
    )),
    'html' => TRUE,
  );
  return $links;
}
function tweetbutton_allowed_path() {
  static $page_match;
  if (isset($page_match)) {
    return $page_match;
  }
  $pages = variable_get('tweetbutton_page_pages', '');
  $visibility = variable_get('tweetbutton_page_visibility', '');

  // Match path if necessary
  if ($pages) {
    if ($visibility < 2) {
      $path = drupal_get_path_alias($_GET['q']);

      // Compare with the internal and path alias (if any).
      $page_match = drupal_match_path($path, $pages);
      if ($path != $_GET['q']) {
        $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
      }
      $page_match = !($visibility xor $page_match);
    }
    else {
      $page_match = drupal_eval($pages);
    }
  }
  else {
    $page_match = TRUE;
  }
  return $page_match;
}
function tweetbutton_get_attributes($entity = NULL, $options = array()) {

  // If data-url is not set then use node url, in teaser mode we want to set this parameter
  // to fetch different counters for each node
  $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', ''),
    'entity_type' => 'node',
    'url' => url('node/' . $entity->nid, array(
      'absolute' => TRUE,
    )),
  );
  $options += $default_option;
  global $language;
  $attributes = array(
    'data-count' => $options['type'],
    'data-via' => $options['account'],
    'data-related' => $options['rel_account'] . ':' . $options['rel_desc'],
    'data-text' => $options['text'] ? token_replace($options['text'], $options['entity_type'], $entity) : '',
    'data-counturl' => $options['url'],
    'data-url' => $options['url'],
    'data-lang' => $options['language'] == 'auto' ? $language->language : $options['language'],
  );
  return $attributes;
}
function theme_tweetbutton_display($entity_id, $options = array()) {
  global $_tweetbutton_init;
  $_tweetbutton_init = TRUE;
  $attributes = tweetbutton_get_attributes($entity_id, $options);
  $link = '<div class="tweetbutton"><a href="http://twitter.com/share" class="twitter-share-button" ' . drupal_attributes($attributes) . '>Tweet</a></div>';
  return $link;
}

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

/**
 * Implementation of hook_views_handlers()
 */
function tweetbutton_views_handlers() {
  return array(
    'handlers' => array(
      'views_handler_field_tweetbutton_node' => array(
        'parent' => 'views_handler_field_tweetbutton',
        'file' => 'tweetbutton_views_field_handlers.inc',
      ),
      'views_handler_field_tweetbutton_user' => array(
        'parent' => 'views_handler_field_tweetbutton',
        'file' => 'tweetbutton_views_field_handlers.inc',
      ),
      'views_handler_field_tweetbutton_taxonomy' => array(
        'parent' => 'views_handler_field_tweetbutton',
        'file' => 'tweetbutton_views_field_handlers.inc',
      ),
      'views_handler_field_tweetbutton' => array(
        'parent' => 'views_handler_field',
        'file' => 'tweetbutton_views_field_handlers.inc',
      ),
    ),
  );
}

/**
 * Implementation of hook_content_extra_fields.
 */
function tweetbutton_content_extra_fields($type) {
  $types = variable_get('tweetbutton_node_types', array(
    'story',
  ));
  $location = variable_get('tweetbutton_node_location', array(
    'full',
  ));
  if (!empty($types[$type]) && empty($location['links'])) {
    $extras['tweetbutton_widget'] = array(
      'label' => t('Tweetbutton'),
      'description' => t('The Tweetbutton widget'),
      'weight' => variable_get('tweetbutton_node_weight', -5),
    );
    return $extras;
  }
}

/**
 * Implementation of hook_footer()
 */
function tweetbutton_footer($main = 0) {
  global $_tweetbutton_init;
  if (!$_tweetbutton_init) {
    return;
  }
  $output = '<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>';
  return $output;
}

Functions

Namesort descending Description
theme_tweetbutton_display
tweetbutton_allowed_path
tweetbutton_content_extra_fields Implementation of hook_content_extra_fields.
tweetbutton_footer Implementation of hook_footer()
tweetbutton_get_attributes
tweetbutton_help Implementation of hook_help()
tweetbutton_link Implementation of hook_link
tweetbutton_menu Implementation of hook_menu()
tweetbutton_nodeapi Implementation of hook_nodeapi()
tweetbutton_perm Implementation of hook_permission()
tweetbutton_theme Implementation of hook_theme()
tweetbutton_views_api Implementation of hook_views_api()
tweetbutton_views_handlers Implementation of hook_views_handlers()