You are here

function twitter_post_node_insert in Twitter 7.4

Same name and namespace in other branches
  1. 7.3 twitter_post/twitter_post.module \twitter_post_node_insert()
  2. 7.5 twitter_post/twitter_post.module \twitter_post_node_insert()

Implementation of hook_node_insert().

Intercepts newly published nodes and posts notices to Twitter.

1 call to twitter_post_node_insert()
twitter_post_node_update in twitter_post/twitter_post.module
Implementation of hook_node_update().

File

twitter_post/twitter_post.module, line 82
Main hooks for twitter post module

Code

function twitter_post_node_insert($node) {
  if (!empty($node->status) && !empty($node->twitter) && !empty($node->twitter['post'])) {
    module_load_include('inc', 'twitter');
    $twitter_account = twitter_account_load($node->twitter['account']);
    $replacements = array(
      '!title' => $node->title,
      '!url' => url('node/' . $node->nid, array(
        'absolute' => TRUE,
        'alias' => TRUE,
      )),
      '!url-alias' => url('node/' . $node->nid, array(
        'absolute' => TRUE,
      )),
      '!user' => $node->name,
    );

    // Only generate the shortened URL if it's going to be used. No sense
    // burning through TinyURLs without a good reason.
    if (strstr($node->twitter['status'], '!tinyurl') !== FALSE) {
      $replacements['!tinyurl'] = twitter_shorten_url(url('node/' . $node->nid, array(
        'absolute' => TRUE,
      )));
    }
    $status = strtr($node->twitter['status'], $replacements);
    if (twitter_set_status($twitter_account, $status)) {
      drupal_set_message(t('Successfully posted to Twitter'));
    }
  }
}