View source
<?php
namespace Drupal\media_entity_twitter\Plugin\media\Source;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\media\MediaInterface;
use Drupal\media\MediaSourceBase;
use Drupal\media\MediaTypeInterface;
use Drupal\media\Plugin\media\Source\OEmbedInterface;
use Drupal\media_entity_twitter\TweetFetcherInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\media\MediaSourceFieldConstraintsInterface;
class Twitter extends MediaSourceBase implements MediaSourceFieldConstraintsInterface, OEmbedInterface {
protected $renderer;
protected $tweetFetcher;
protected $logger;
protected $fileSystem;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('plugin.manager.field.field_type'), $container
->get('config.factory'), $container
->get('renderer'), $container
->get('media_entity_twitter.tweet_fetcher'), $container
->get('logger.factory')
->get('media_entity_twitter'), $container
->get('file_system'));
}
public static $validationRegexp = [
'@((http|https):){0,1}//(www\\.){0,1}twitter\\.com/(?<user>[a-z0-9_-]+)/(status(es){0,1})/(?<id>[\\d]+)@i' => 'id',
];
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory, RendererInterface $renderer, TweetFetcherInterface $tweet_fetcher, LoggerChannelInterface $logger, FileSystemInterface $file_system) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory);
$this->renderer = $renderer;
$this->tweetFetcher = $tweet_fetcher;
$this->logger = $logger;
$this->fileSystem = $file_system;
}
public function defaultConfiguration() {
return [
'source_field' => '',
'use_twitter_api' => FALSE,
'generate_thumbnails' => FALSE,
'consumer_key' => '',
'consumer_secret' => '',
'oauth_access_token' => '',
'oauth_access_token_secret' => '',
];
}
public function getMetadataAttributes() {
$attributes = [
'id' => $this
->t('Tweet ID'),
'user' => $this
->t('Twitter user information'),
];
if ($this->configuration['use_twitter_api']) {
$attributes += [
'image' => $this
->t('Link to the twitter image'),
'image_local' => $this
->t('Copies tweet image to the local filesystem and returns the URI.'),
'image_local_uri' => $this
->t('Gets URI of the locally saved image.'),
'content' => $this
->t('This tweet content'),
'retweet_count' => $this
->t('Retweet count for this tweet'),
'profile_image_url_https' => $this
->t('Link to profile image'),
'created_time' => $this
->t('Date/time created'),
'user_name' => $this
->t('User name'),
];
}
return $attributes;
}
public function getMetadata(MediaInterface $media, $attribute_name) {
$matches = $this
->matchRegexp($media);
if (!is_array($matches) || empty($matches['id'])) {
return NULL;
}
switch ($attribute_name) {
case 'id':
return $matches['id'];
case 'user':
return $matches['user'] ?: NULL;
case 'thumbnail_uri':
if ($local_image = $this
->getMetadata($media, 'image_local')) {
return $local_image;
}
if (empty($this->configuration['generate_thumbnails'])) {
return parent::getMetadata($media, $attribute_name);
}
$id = $this
->getMetadata($media, 'id');
$thumbnail_uri = $this
->getLocalImageUri($id, $media);
if (file_exists($thumbnail_uri)) {
return $thumbnail_uri;
}
$thumbnail = [
'#theme' => 'media_entity_twitter_tweet_thumbnail',
'#content' => $this
->getMetadata($media, 'content'),
'#author' => $this
->getMetadata($media, 'user'),
'#avatar' => $this
->getMetadata($media, 'profile_image_url_https'),
];
$svg = $this->renderer
->renderRoot($thumbnail);
return $this->fileSystem
->saveData($svg, $thumbnail_uri, FileSystemInterface::EXISTS_ERROR) ?: parent::getMetadata($media, $attribute_name);
}
if ($this->configuration['use_twitter_api'] && ($tweet = $this
->fetchTweet($matches['id']))) {
switch ($attribute_name) {
case 'image':
if (isset($tweet['extended_entities']['media'][0]['media_url'])) {
return $tweet['extended_entities']['media'][0]['media_url'];
}
return NULL;
case 'image_local':
$local_uri = $this
->getMetadata($media, 'image_local_uri');
if ($local_uri) {
if (file_exists($local_uri)) {
return $local_uri;
}
else {
$image_url = $this
->getMetadata($media, 'image');
$image_data = file_get_contents($image_url);
if ($image_data) {
return $this->fileSystem
->saveData($image_data, $local_uri, FileSystemInterface::EXISTS_REPLACE);
}
}
}
return NULL;
case 'image_local_uri':
$image_url = $this
->getMetadata($media, 'image');
if ($image_url) {
return $this
->getLocalImageUri($matches['id'], $media, $image_url);
}
return NULL;
case 'content':
if (isset($tweet['full_text'])) {
return $tweet['full_text'];
}
return NULL;
case 'retweet_count':
if (isset($tweet['retweet_count'])) {
return $tweet['retweet_count'];
}
return NULL;
case 'profile_image_url_https':
if (isset($tweet['user']['profile_image_url_https'])) {
return $tweet['user']['profile_image_url_https'];
}
return NULL;
case 'created_time':
if (isset($tweet['created_at'])) {
if ($datetime = DrupalDateTime::createFromFormat('D M d H:i:s O Y', $tweet['created_at'])) {
return $datetime
->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
}
}
return NULL;
case 'user_name':
if (isset($tweet['user']['name'])) {
return $tweet['user']['name'];
}
return NULL;
case 'default_name':
$user = $this
->getMetadata($media, 'user');
$id = $this
->getMetadata($media, 'id');
if (!empty($user) && !empty($id)) {
return $user . ' - ' . $id;
}
return NULL;
}
}
return NULL;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['use_twitter_api'] = [
'#type' => 'select',
'#title' => $this
->t('Whether to use Twitter api to fetch tweets or not.'),
'#description' => $this
->t("In order to use Twitter's api you have to create a developer account and an application. For more information consult the readme file."),
'#default_value' => empty($this->configuration['use_twitter_api']) ? 0 : $this->configuration['use_twitter_api'],
'#options' => [
0 => $this
->t('No'),
1 => $this
->t('Yes'),
],
];
$form['consumer_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('Consumer key'),
'#default_value' => empty($this->configuration['consumer_key']) ? NULL : $this->configuration['consumer_key'],
'#states' => [
'visible' => [
':input[name="source_configuration[use_twitter_api]"]' => [
'value' => '1',
],
],
],
];
$form['consumer_secret'] = [
'#type' => 'textfield',
'#title' => $this
->t('Consumer secret'),
'#default_value' => empty($this->configuration['consumer_secret']) ? NULL : $this->configuration['consumer_secret'],
'#states' => [
'visible' => [
':input[name="source_configuration[use_twitter_api]"]' => [
'value' => '1',
],
],
],
];
$form['oauth_access_token'] = [
'#type' => 'textfield',
'#title' => $this
->t('Oauth access token'),
'#default_value' => empty($this->configuration['oauth_access_token']) ? NULL : $this->configuration['oauth_access_token'],
'#states' => [
'visible' => [
':input[name="source_configuration[use_twitter_api]"]' => [
'value' => '1',
],
],
],
];
$form['oauth_access_token_secret'] = [
'#type' => 'textfield',
'#title' => $this
->t('Oauth access token secret'),
'#default_value' => empty($this->configuration['oauth_access_token_secret']) ? NULL : $this->configuration['oauth_access_token_secret'],
'#states' => [
'visible' => [
':input[name="source_configuration[use_twitter_api]"]' => [
'value' => '1',
],
],
],
];
$form['generate_thumbnails'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Generate thumbnails'),
'#default_value' => $this->configuration['generate_thumbnails'],
'#states' => [
'visible' => [
':input[name="source_configuration[use_twitter_api]"]' => [
'checked' => TRUE,
],
],
],
'#description' => $this
->t('If checked, Drupal will automatically generate thumbnails for tweets that do not reference any external media. In certain circumstances, <strong>this may violate <a href="@policy">Twitter\'s fair use policy</a></strong>. Please <strong>read it and be careful</strong> if you choose to enable this.', [
'@policy' => 'https://dev.twitter.com/overview/terms/agreement-and-policy',
]),
];
return $form;
}
public function getSourceFieldConstraints() {
return [
'oembed_resource' => [],
];
}
public function createSourceField(MediaTypeInterface $type) {
return parent::createSourceField($type)
->set('label', 'Tweet URL');
}
protected function getLocalImageUri($id, MediaInterface $media, $media_url = NULL) {
$directory = $this->configFactory
->get('media_entity_twitter.settings')
->get('local_images');
$ready = $this->fileSystem
->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
if (!$ready) {
$this->logger
->warning('Could not prepare thumbnail destination directory @dir', [
'@dir' => $directory,
]);
return parent::getMetadata($media, 'thumbnail_uri');
}
$local_uri = $directory . '/' . $id . '.';
if ($media_url) {
$local_uri .= pathinfo($media_url, PATHINFO_EXTENSION);
}
else {
$local_uri .= 'svg';
}
return $local_uri;
}
protected function matchRegexp(MediaInterface $media) {
$matches = [];
$source_field = $this
->getSourceFieldDefinition($media->bundle->entity)
->getName();
if ($media
->hasField($source_field) && !$media
->get($source_field)
->isEmpty()) {
$property_name = $media
->get($source_field)
->first()
->mainPropertyName();
foreach (static::$validationRegexp as $pattern => $key) {
if (preg_match($pattern, $media
->get($source_field)->{$property_name}, $matches)) {
return $matches;
}
}
}
return FALSE;
}
protected function fetchTweet($id) {
$this->tweetFetcher
->setCredentials($this->configuration['consumer_key'], $this->configuration['consumer_secret'], $this->configuration['oauth_access_token'], $this->configuration['oauth_access_token_secret']);
return $this->tweetFetcher
->fetchTweet($id);
}
public function getProviders() {
return [
'Twitter',
];
}
}