Social Post Twitter - Installation instructions

Last updated on
9 March 2017

Social Post Twitter module depends on TwitterOAuth library and uses Composer to download it. Composer can automatically download the correct version of the library for you.

Overview of installation steps

  1. Step 1: Create an app and get credentials from https://apps.twitter.com/
  2. Step 2: Download Social Post Twitter to the /modules directory of your Drupal installation but do NOT enable the module yet.
  3. Step 3: Download and initialize Composer Manager to the /modules directory.
  4. Step 4: Let Composer download TwitterOAuth library for you.
  5. Step 5: Enable Social Post Twitter module and configure it

Step 1: Create a Twitter App

  • Create a new app on https://apps.twitter.com
  • The app will ask for a callback. The callback function is called by /user/social-post/twitter/auth/callback. However, you do not need to specify a valid callback url as Twitter does not check if this value is matched in the request. Just make sure it is not a local url, like localhost

Step 2: Download Social Post Twitter and TwitterOAuth library using composer

On the the command line of your server, navigate to your Drupal root directory.

Let's inform Composer that we want to use packages.drupal.org/8 as a package repository:

composer config repositories.drupal composer https://packages.drupal.org/8

Now that Composer can search packages from Drupal 8 package repository, let Composer download Social Post Twitter (and all its dependencies). The following command will download the latest 8.x-1.0 release of Social Post Twitter:

composer require "drupal/social_post_twitter:~1.0"

If you would like to use the 8.x-1.x-dev instead, use the following command:

composer require "drupal/social_api:1.x-dev" "drupal/social_auth:1.x-dev" "drupal/social_post_twitter:1.x-dev"

Social Post Twitter dev versions are usually only compatible with Social Post and Social API dev versions. The command above installs the dev branch of all these modules.

Step 5: Enable and configure Social Post Twitter module

Now that Composer has downloaded the TwitterOAuth library, you can enable Social Post Twitter module.

  • The module settings can be configured on /admin/config/social-api/social-post/twitter.
  • Add your Consumer Key and Consumer Secret (from step 1) to the module settings.
  • You probably also want to check your Drupal account settings at /admin/config/people/accounts and provide permission for specified roles to autotweet.

User Setup

Once the module is installed and configured, users can set up their account so they can tweet.

  • Users who have a role with permission to autotweet will find a "Social Post Twitter Add Account" button in their My Account Edit page (user/{user_id}/edit).
  • Users click on this Add Account button which redirects to the Twitter page specific for your app (configured in Step 1) and enter their credentials. If successful, they are redirected back to the user/edit page where their Twitter screen name will now be shown.
  • Tweeting is organised by user,  each having an independent account on Twitter and whose credentials are not known to the Drupal site administrator. If you want your site to Tweet based on some administrative event rule then you need to set up a site user (e.g.: twitter_manager) as a separate Twitter user using the steps above.
  • The site admin can see which users have set up accounts by looking at admin/config/social-api/social-post/twitter/users.

Step 5: Tweet!

You can update twitter status using the TwitterPost object and the Social Post Twitter Entity

/* @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 Twitter entity storage.
$twitterEntity = \Drupal::entityTypeManager()->getStorage('social_post_twitter_user');

/* @var \Drupal\social_post_twitter\Entity\TwitterUserInterface[] $accounts */
// Gets the Twitter accounts associated with the current user.
$accounts = $twitterEntity->loadByProperties([
  'uid' => Drupal::currentUser()->id(),
]);

// Loop through each Twitter accounts.
foreach ($accounts as $account) {
  $twitterPost->doPost($account->getAccessToken(), $account->getAccessTokenSecret(), "My tweet");
}

Rules

You can alternatively use Rules to perform Tweet actions without writing any lines of code.

You can add Reaction Rules, specifying the event (e.g.: After saving new content) and conditions. Then, you can add the action Tweet and add the status text value. For example, putting the value "{{ node.title.value }}" fires the Rule Action when new content is created and tweets the title of the new content.

Support requests

  • Before posting a support request, carefully read the installation instructions provided on this page and do the setup in this order.
  • Once you have done this, you can post a support request at module issue queue. Support requests to the issue queue, please, not to the comments on this documentation page.
  • When posting a support request, please inform if you were able to see any errors in Recent log entries.