You are here

function _tweet_to_twitter in Tweet 6

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.2 tweet.module \_tweet_to_twitter()
  5. 6.3 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.

$title: If FALSE, no page title will be included in the twitter message; if TRUE, the pagetitle will be included and will be determined from the current page or the NID passed to the function. If $title is a string, the string will be used as the title. @title is replaced with the current page's title or the title as determined from the NID passed to the function. If 1, the default from the settings page will be used.

$nid: The NID of the node for which the twitter link should be constructed.

$q: The absolute URL of the page for which the twitter link should be constructed. If this is not the current page, the title must be set manually, or it will be incorrect.

Return value

A themed link to post the specified or current page to twitter, optionally including the page title.

See also

_tweet_make_url()

_tweet_get_title()

tweet_to_twitter()

tweet_link()

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 92
Builds links to post pages to twitter.

Code

function _tweet_to_twitter($type = '', $title = 1, $nid = '', $q = '') {
  $hashtag = '';
  if (variable_get('tweet_hashtag', '')) {

    //It is necessary to manually escape the # character because if unescaped it will not be included in the message and if drupal_urlencoded() it gets encoded twice.
    $hashtag = ' ' . str_replace('#', '%23', variable_get('tweet_hashtag', ''));
  }
  if ($nid && !$q) {
    global $base_url, $base_root, $base_path;
    $alias = drupal_get_path_alias('node/' . $nid);
    $q = $base_root . $base_path . $alias;
  }
  $tweet = _tweet_make_url($q);
  if ($title === 1 || $title === '1') {
    $title = variable_get('tweet_title', 1);
  }
  if ($title) {
    if (is_string($title) && $title !== '1') {
      $title = str_replace('@title', _tweet_get_title($nid), $title);
      $tweet .= ' ' . $title . $hashtag;
    }
    else {
      $tweet .= ' ' . _tweet_get_title($nid) . $hashtag;
    }
  }
  else {

    //Add a space even though titles are not included so the user can begin typing a description right away.
    $tweet .= ' ';
  }
  $path = 'http://twitter.com/home';
  $text = t('Post to Twitter');
  $image_location = drupal_get_path('module', 'tweet') . '/icon.png';
  $image = '<img src="' . $base_url . '/' . variable_get('tweet_image', $image_location) . '" alt="' . $text . '" title="' . $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;
  }
  else {
    if ($type == 'icon_text') {
      $show = $image . ' ' . $text;
    }
    else {
      if ($type == 'text') {
        $show = $text;
      }
    }
  }
  $attributes = array(
    'class' => 'tweet',
    'rel' => 'nofollow',
  );
  if (variable_get('tweet_new_window', 'target') == 'target') {
    $attributes['target'] = '_blank';
  }
  else {
    if (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,
  );
}