You are here

function _tweet_to_twitter in Tweet 5

Same name and namespace in other branches
  1. 5.2 tweet.module \_tweet_to_twitter()
  2. 6.4 tweet.module \_tweet_to_twitter()
  3. 6 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 and it is not empty, 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. Avoid using this if you're already on the page you want to build the link for, because it may cause the incorrect page title to be used if certain modules are installed.

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

Code

function _tweet_to_twitter($type = '', $title = 1, $nid = '', $q = '') {
  if ($nid && !$q) {
    global $base_url;
    $alias = drupal_get_path_alias('node/' . $nid);
    $q = $base_url . 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;
    }
    else {
      $tweet .= ' ' . _tweet_get_title($nid);
    }
  }
  else {

    //If titles are not included, add a space anyway so the user can begin typing a description right away.
    $tweet .= ' ';
  }
  $tweet = drupal_urlencode($tweet);
  $path = 'http://twitter.com/home';
  $text = t('Post to Twitter');
  $image = '<img src="http://assets1.twitter.com/images/favicon.ico" alt=' . $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(
    'target' => '_blank',
    'class' => 'tweet',
  );
  return array(
    'title' => $show,
    'href' => $path,
    'attributes' => $attributes,
    'query' => 'status=' . $tweet,
    'html' => TRUE,
  );
}