Scroll
Drupal 8 での Composer。Twitter から最新のツイートを表示する。
Drupal でモジュールやライブラリをインストールするには、Composer を使うのが最善です。Composer は依存関係マネージャーで、モジュールをインストールする際に必要なライブラリを自動的にインストールします。Composer はモジュールのインストールを少し複雑にしますが、外部ライブラリを自動的にダウンロードして接続するので、それらの管理を簡素化します。
Composer をインストールするには、公式サイトに移動します:
https://getcomposer.org/download/
Composer 経由で Drush をインストールしていれば、Composer はすでにインストールされています。Composer がインストールされていることを確認するには、次を実行します:
composer --version
確認として Composer のバージョンが表示されるはずです。
Drupal 8 で最新のツイートを表示する
まず、Twitter アプリケーションを作成し、必要な API トークンを次で取得します:
以前のバージョンは非推奨なので、必ず API v1.1 以上を使ってください。アプリを作成し、次を生成します:
- Consumer Key
- Consumer Secret
- Access Token
- Access Token Secret
Twitter API とやり取りするために、PHP ライブラリ j7mbo/twitter-api-php を使います。Composer 経由でインストールします:
composer require j7mbo/twitter-api-php
次に、/modules/twitter_block に次のファイルでカスタムモジュールを作成します:
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 (モジュールの依存関係)
{
"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"
}