سؤال

Twitter Module is working finde with my Drupal 7 site. I wanted to make a tweek so that nodes that are not hidden get tweeted, I was able to do this by altering twitter_post_node_insert in twitter/twitter_post . All I did was add a new condition of !$node->hidden . It works great.

function twitter_post_node_insert($node) {
   if (!empty($node->status) && !empty($node->twitter) && !empty($node->twitter['post']) 
         && !$node->hidden) { ......

My problem is that this code in the Twitter Module only gets called when I directly edit a node and save it. Now, I would like to have said code called also when I edit my node programmatically, where I save it with $node_wrapper->save(); . The twitter code won't get called. I've also tried with node_save($node); , instead of using my $node_wrapper. Nothing.

I also tried including the file twitter_post.module located in twitter/twitter_post, and then calling the function in charge of posting the tweet :

 module_load_include('module', 'twitter', '../twitter/twitter_post/twitter_post');
 twitter_post_node_update($node);

Nothing happens and no errors are shown. What I'd like is to know what Drupal 7 function gets called in its core when you edit a node through its interface and then save it. That way I can just put that function in the code where I edit my node programmatically so that the Twitter code will also get called. Or, does anyone have a better approach?

Thanks.

هل كانت مفيدة؟

المحلول

After looking into the Twitter module this is what I have done in order to have the nodes published programmatically.

I added the following function to twitter/twitter_post/twitter_post.module . It's a copy of the function function twitter_post_node_insert($node), found on the same file. I made a copy so that it would not print out the message of "tweet posted succesfully". That way I call the copied function from another place to post the tweet.

/**
 * Function called  from custom .module to insert tweets from "Editar Pesos" tab
*/

function twitter_post_node_custom_insert($node) {
   if (!empty($node->status) && !empty($node->twitter) && !empty($node->twitter['post']) && !$node->hidden ) {
    module_load_include('inc', 'twitter');

   $twitter_account = twitter_account_load($node->twitter['account']);
   $replacements = array(
     '!title' => truncate_utf8($node->title, 90, false, true),
      '!url' => url('node/' . $node->nid, array('absolute' => TRUE, 'alias' => TRUE)),
     '!url-alias' => url('node/' . $node->nid, array('absolute' => TRUE))
   );

   // 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);
   return twitter_set_status($twitter_account, $status);
  }
}

And following is the "magic", which gets called whenever I want the node posted as a tweet.

function post_to_twitter($node){
    module_load_include('module', 'twitter', '../twitter/twitter_post/twitter_post');

     $twitter = array(
              'account' => getTwitterUid(),
              'post' => 'POST',
              'status' => "!title !tinyurl"
           );

  $node->twitter = $twitter;

  return twitter_post_node_custom_insert($node);
}


function getTwitterUid(){
 return db_query("select twitter_uid from {twitter_account} where screen_name = :screen_name 
     limit 1", array(":screen_name" => 'YOUR_TwitterScreenName'))->fetchField();
}

Hope this can help anyone who was looking for the same thing as I.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top