Composer in Drupal 8. Displaying latest tweets from Twitter.
To install modules and libraries in Drupal, it is best to use Composer. Composer is a dependency manager that automatically installs required libraries when installing a module. Although Composer makes module installation slightly more complex, it simplifies managing external libraries since it downloads and connects them automatically.
To install Composer, go to the official site:
https://getcomposer.org/download/
If you've installed Drush via Composer, then Composer is already installed. To verify Composer is installed, run:
composer --version
You should see the Composer version as confirmation.
Displaying Latest Tweets in Drupal 8
First, create a Twitter application and obtain the necessary API tokens at:
Ensure you use API v1.1 or higher, as earlier versions are deprecated. Create your app, and then generate the following:
- Consumer Key
- Consumer Secret
- Access Token
- Access Token Secret
We’ll use the PHP library j7mbo/twitter-api-php
to interact with the Twitter API. Install it via Composer:
composer require j7mbo/twitter-api-php
Next, create a custom module in /modules/twitter_block
with these files:
twitter_block.info.yml
name: Twitter Block
description: Display Last tweets.
type: module
core: 8.x
package: Custom
TwitterBlock.php (Block Plugin)
<?php
namespace Drupal\twitter_block\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* @Block(
* id = "twitter_block",
* admin_label = @Translation("Twitter block"),
* )
*/
class TwitterBlock extends BlockBase {
public function build() {
$config = \Drupal::config('twitter_block.settings');
$settings = [
'consumer_key' => $config->get('consumer_key'),
'consumer_secret' => $config->get('consumer_secret'),
'oauth_access_token' => $config->get('access_token'),
'oauth_access_token_secret' => $config->get('access_token_secret'),
];
$screen_name = 'netglooweb';
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = "?count=1";
$requestMethod = 'GET';
$twitter = new \TwitterAPIExchange($settings);
$user_timeline = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$messages = json_decode($user_timeline);
$content = '';
if (!empty($messages)) {
foreach ($messages as $message) {
$content .= '<div class="twitter-message">' . $message->text . '</div>';
}
}
return ['#markup' => $content];
}
}
TwitterBlockSettingsForm.php
<?php
namespace Drupal\twitter_block\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
class TwitterBlockSettingsForm extends ConfigFormBase {
public function getFormId() {
return 'twitter_block_admin_settings';
}
protected function getEditableConfigNames() {
return ['twitter_block.settings'];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('twitter_block.settings');
$form['consumer_key'] = ['#type' => 'textfield', '#title' => $this->t('Consumer key'), '#default_value' => $config->get('consumer_key')];
$form['consumer_secret'] = ['#type' => 'textfield', '#title' => $this->t('Consumer secret'), '#default_value' => $config->get('consumer_secret')];
$form['access_token'] = ['#type' => 'textfield', '#title' => $this->t('Access token'), '#default_value' => $config->get('access_token')];
$form['access_token_secret'] = ['#type' => 'textfield', '#title' => $this->t('Access token secret'), '#default_value' => $config->get('access_token_secret')];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('twitter_block.settings')
->set('consumer_key', $form_state->getValue('consumer_key'))
->set('consumer_secret', $form_state->getValue('consumer_secret'))
->set('access_token', $form_state->getValue('access_token'))
->set('access_token_secret', $form_state->getValue('access_token_secret'))
->save();
parent::submitForm($form, $form_state);
}
}
twitter_block.routing.yml
twitter_block.settings:
path: '/admin/structure/twitter-block/settings'
defaults:
_form: '\Drupal\twitter_block\Form\TwitterBlockSettingsForm'
_title: 'Twitter API Settings'
requirements:
_permission: 'administer site configuration'
composer.json (Module Dependency)
{
"name": "drupal/twitter_block",
"type": "drupal-module",
"description": "Displays Last tweets.",
"homepage": "https://drupalbook.org/ru/drupal/composer-v-drupal-8-vyvod-poslednih-tvitov-iz-twitter",
"license": "GPL-2.0+",
"require": {
"j7mbo/twitter-api-php": "dev-master"
},
"minimum-stability": "dev"
}