#! code: Drupal 9: Auto Tweeting From A Drupal Site When Content Is Published

Normally, when creating Tweets from Drupal 8 I use the Social Post Twitter module. This module is part of the Drupal Social Initiative and has been my go-to module when I’ve needed to read or send Tweets from a Drupal site. Since the release of Drupal 9, however, these modules have not been receiving the support needed and as of writing this article there is no easy way to install them on a Drupal 9 site. I’ve looked into the issue queues and can’t see why the delay is there.

The Social Post Twitter module does have a lot of features that I didn’t need for what I was looking for, which was to send Tweets when items of content are created. I decided to see how difficult it would be to send Tweets from a Drupal site as an item of content is published.

As it happens, creating your own Twitter integration to post status updates to Twitter isn’t that difficult. The Social Post Twitter module is build around the abraham/twitteroauth package. Aside from the boilerplate code of setting up services and hooks I only needed a few lines of code to start that package and then send the needed data to Twitter. I decided it would be helpful to go through every step along the way from getting authentication details to writing code and creating a Drupal module.

Before getting into how to get the code working we need to get some authentication details.

Getting Your Access Tokens

In order to send Tweets from your Drupal site you’ll need to get some OAuth details. Do find these you need to head over to https://developer.twitter.com/ and apply for developer access. This isn’t that easy to figure out how to do this, so I’ve added some steps here to go through.

Read more.


Go to Source
Author: