Use case:

For Drupalchix, I want to pull in several users' Twitter feeds via Feed API and re-tweet them over a single account, so all nodes of a "tweet" type should be posted to Twitter.

It'd be convenient if there was an option to always post a particular content type to Twitter, rather than having to check a box. This is especially relevant because Feed API creates nodes through node_object_prepare() and node_save(), rather than drupal_execute(), so this code never gets fired.

In the meantime, here's a small hook_nodeapi() implementation that does this for someone who needs this functionality now.

/**       
 * Implementation of hook_nodeapi().
 */
function drupalchix_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'insert': 
      if ($node->type == 'tweet') {
        module_load_include('inc', 'twitter');
        $twitter_accounts = drupal_map_assoc(array_keys(twitter_get_user_accounts($node->uid, TRUE)));
        $node->twitter['post'] = TRUE;
        $node->twitter['status'] = 'RT @!title';
        $node->twitter['account'] = array_pop(array_keys($twitter_accounts));
      }
      break;
  } 
}  

Comments

Leeteq’s picture

+1

rudygodoy’s picture

+1

summit’s picture

Hi, where to add this in the twitter module? Is beneath code correct:

  switch ($op) {
    case 'insert':
    case 'update':
       if ($node->type == 'tweet') {
        module_load_include('inc', 'twitter');
        $twitter_accounts = drupal_map_assoc(array_keys(twitter_get_user_accounts($node->uid, TRUE)));
        $node->twitter['post'] = TRUE;
        $node->twitter['status'] = 'RT @!title';
        $node->twitter['account'] = array_pop(array_keys($twitter_accounts));
      }
      else {
        if (!empty($node->status) && !empty($node->twitter) && !empty($node->twitter['post'])) {
          $twitter_accounts = twitter_get_user_accounts($node->uid, TRUE);
          $pass = $twitter_accounts[$node->twitter['account']]['password'];
  
          $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);
          $result = twitter_set_status($node->twitter['account'], $pass, $status);
          if (_twitter_request_failure($result)) {
            drupal_set_message(t('An error occurred when posting to twitter: %code %error',
                                 array('%code' => $result->code, '%error' => $result->error)), 'warning');
          }
          else {
            drupal_set_message(t('Successfully posted to Twitter'));
          }
        }
      }
      break;

I have the same situation as feedapi but with another content type. Story and page nodes the "Post to twitter.com" is shown, but for example weblinks not.

greetings, Martijn

nancydru’s picture

Out of curiosity, since Summit also posted a similar request to the Web Links queue, why can the Twitter module not correctly handle a standard node creation mechanism (node/add/mytype)?

raintonr’s picture

+1

Our use case is that when a community member (user on the Drupal site) posts a new 'event' type on the Drupal site, that this is tweeted on an account that belongs to the site.

Eg. we have a twitter account 'Our organisation' that should have a list of tweets with links to all events.

Side issue: Moderators should optionally be able tweet any content on an account that belongs to the site, not their personal twitter. That appears OT for this issue though.

yngens’s picture

I'd like to use blog api with twitter module to tweet story type documents. Where exactly the code above should go into?

socialnicheguru’s picture

is this part of dev 3?

Is there a way to just set this on the content type page?

xurizaemon’s picture

Version: 6.x-2.x-dev » 7.x-3.x-dev
Status: Active » Postponed

No way to set this on the content page at this point.

Issue has languished for a long time, and there's a working solution above so will postpone until someone contributes a patch.

juampynr’s picture

Status: Postponed » Closed (won't fix)

Closing.

raintonr’s picture

Status: Closed (won't fix) » Active

While there may be code snippets in this thread that give a clue as to how this could be done, I do no see that warrants the issue being closed.

It is obviously a sought after feature and not all users are technical enough to implement code so re-opening.

juampynr’s picture

Status: Active » Fixed

@raintor, I think that you can achieve this with the twitter_actions submodule.

1. Enable twitter_actions
2. authenticate a Twitter account and make it global
3. Create a Rule that triggers when new content of type foo is created.
4. Set up a Post to Twitter reaction using the global account.

Let me know if this works for you.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.