You are here

function _tweet_to_twitter in Tweet 6.3

Same name and namespace in other branches
  1. 5.2 tweet.module \_tweet_to_twitter()
  2. 5 tweet.module \_tweet_to_twitter()
  3. 6.4 tweet.module \_tweet_to_twitter()
  4. 6 tweet.module \_tweet_to_twitter()
  5. 6.2 tweet.module \_tweet_to_twitter()
  6. 7.4 tweet.module \_tweet_to_twitter()

Creates a link to post a URL and optionally title to twitter. Uses the current page by default.

Parameters

$type: Specifies what will show up in the link: the twitter icon, the twitter icon and text, or just text. Pass 'icon' to show just the icon, 'icon_text' to show the icon and text, and 'text' to show just the text. Required if display options for nodes are set to 'none' on the settings page.

$format: A string representing the tweet text, optionally with the case-insensitive tokens [url], [title], and [node-tags]. If not passed, the format from the settings page will be used.

$nid: The NID of the node for which the twitter link should be constructed, or the absolute URL of the page for which the twitter link should be constructed. If the URL given is not the current URL, and if $nid is not a NID, the title must be set manually (instead of using the [title] token) or it will be incorrect.

Return value

A themed link to post the specified or current page to twitter.

2 calls to _tweet_to_twitter()
tweet_link in ./tweet.module
Implementation of hook_link().
tweet_to_twitter in ./tweet.module
Returns a link from _tweet_to_twitter().

File

./tweet.module, line 108
Builds links to post pages to twitter.

Code

function _tweet_to_twitter($type = '', $format = '', $nid = '') {
  $q = '';
  $node_tags = '';
  if (is_numeric($nid)) {
    $q = url('node/' . $nid, array(
      'absolute' => TRUE,
    ));
    if (module_exists('taxonomy')) {
      $node_tag_vocabs = variable_get('tweet_node_vocabs', array());
      foreach ($node_tag_vocabs as $vid => $enabled) {
        if ($enabled) {
          $node = node_load(array(
            'nid' => $nid,
          ));
          $terms = taxonomy_node_get_terms_by_vocabulary($node, $vid);
          foreach ($terms as $term) {
            $node_tags .= ' #' . check_plain($term->name);
          }
        }
      }
    }
  }
  $url = $q;
  if (module_exists('shorten')) {
    $url = shorten_url($q);
  }
  $title = _tweet_get_title($nid);
  if (!$format) {
    $format = variable_get('tweet_format', '[url] [title][node-tags]');
  }
  $tweet = _tweet_process($format, array(
    '[url]' => $url,
    '[title]' => $title,
    '[node-tags]' => check_plain($node_tags),
  ));
  $path = 'http://twitter.com/home';
  $text = variable_get('tweet_text', t('Post to Twitter')) == t('Post to Twitter') ? t('Post to Twitter') : variable_get('tweet_text', t('Post to Twitter'));
  $image_location = drupal_get_path('module', 'tweet') . '/icon.png';
  $image = theme('image', variable_get('tweet_image', $image_location), $text, $text);
  if (!$type) {

    //Note that $type can be 'none', in which case nothing shows up.
    $type = variable_get('tweet_node', 'icon');
  }
  if ($type == 'icon') {
    $show = $image;
  }
  elseif ($type == 'icon_text') {
    $show = $image . ' ' . $text;
  }
  elseif ($type == 'text') {
    $show = $text;
  }
  $attributes = array(
    'class' => 'tweet',
    'rel' => 'nofollow',
  );
  if (variable_get('tweet_new_window', 'target') == 'target') {
    $attributes['target'] = '_blank';
  }
  elseif (variable_get('tweet_new_window', 'target') == 'js') {
    $attributes['onclick'] = "window.open('{$path}?status={$tweet}','twitter','')";
    $path = $_GET['q'];
    $tweet = 'sent';
  }
  return array(
    'title' => $show,
    'href' => $path,
    'attributes' => $attributes,
    'query' => 'status=' . $tweet,
    'html' => TRUE,
  );
}