To help with, I wrote a full use case article for Drupal 9 (also working on Drupal 8) on how I use it currently in a production website. The article is here: https://freelance-drupal.com/en/blog/automatically-tweet-published-content

Comments

gvso created an issue. See original summary.

gvso’s picture

Status: Active » Fixed

This is how to tweet content using the API:

/* @var \Drupal\social_post_twitter\Plugin\Network\TwitterPostInterface $post */
// Gets instance of Social Post Twitter.
$post = \Drupal::service('plugin.network.manager')->createInstance('social_post_twitter');

// Gets the Social Post manager.
/* @var \Drupal\social_post\SocialPostManager $post_manager */
$post_manager = \Drupal::service('social_post.post_manager');

$post_manager->setPluginId('social_post_twitter');

$current_user = \Drupal::service('current_user');

$accounts = $post_manager->getAccountsByUserId('social_post_twitter', $current_user->id());

$status = "This is a test";

/* @var \Drupal\social_post\Entity\SocialPost $account */
foreach ($accounts as $account) {
  $access_token = json_decode($post_manager->getToken($account->getProviderUserId()), TRUE);
  $post->doPost($access_token['oauth_token'], $access_token['oauth_token_secret'], $status);
}

Rules can be used too!

gvso’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev

Status: Fixed » Closed (fixed)

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

joshua.boltz’s picture

Using the patch from https://www.drupal.org/project/social_post_twitter/issues/2975062#commen..., it's possible to upload and attach media to a tweet for 2.x:

/* @var \Drupal\social_post_twitter\Plugin\Network\TwitterPostInterface $twitterPost */
  // Gets instance of Social Post Twitter.
  $twitterPost = \Drupal::service('plugin.network.manager')
    ->createInstance('social_post_twitter');

  // Gets the Social Post manager.
  /* @var \Drupal\social_post\SocialPostManager $post_manager */
  $post_manager = \Drupal::service('social_post.post_manager');

  $post_manager->setPluginId('social_post_twitter');

  $current_user = \Drupal::service('current_user');

  $accounts = $post_manager->getAccountsByUserId('social_post_twitter', $current_user->id());

  $tweet = [];

  // Use the value from this field as the 'status' text to tweet.
  if ($node->hasField('field_twitter_post')) {
    try {
      $twitter_post_value = $node->get('field_twitter_post')->first();
    }
    catch (MissingDataException $e) {
      \Drupal::logger('my_module')->error($e->getMessage());
    }

    if (!empty($twitter_post_value)) {
      $tweet['status'] = $twitter_post_value->getValue();
    }
  }

  // Use field_image as source images for media posting to Twitter.
  $images = ($node->hasField('field_image') && !empty($node->get('field_image')
    ->getValue())) ? $node->get('field_image')
    ->referencedEntities() : [];

  $image_paths = [];
  foreach ($images as $image) {
    $image_paths[] = drupal_realpath($image->getFileUri());
  }

  $tweet['media_paths'] = $image_paths;

  if (!empty($tweet)) {
    // Loop through each Twitter accounts.
    foreach ($accounts as $account) {
      $access_token = Json::decode($post_manager->getToken($account->getProviderUserId()));
      $twitterPost->doPost($access_token['oauth_token'], $access_token['oauth_token_secret'], $tweet);
    }
  }
dom.’s picture

Issue summary: View changes