View source
<?php
namespace Drupal\bynder\Plugin\media\Source;
use Drupal\bynder\BynderApiInterface;
use Drupal\bynder\Plugin\Field\FieldType\BynderMetadataItem;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\media\MediaInterface;
use Drupal\media\MediaSourceBase;
use Drupal\media\MediaTypeInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Bynder extends MediaSourceBase {
protected $bynderApi;
protected $accountProxy;
protected $urlGenerator;
protected $metadata;
protected $logger;
protected $cache;
protected $time;
protected $moduleHandler;
protected static $timoutDetected = FALSE;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory, BynderApiInterface $bynder_api_service, AccountProxyInterface $account_proxy, UrlGeneratorInterface $url_generator, LoggerChannelFactoryInterface $logger, CacheBackendInterface $cache, TimeInterface $time, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory);
$this->bynderApi = $bynder_api_service;
$this->accountProxy = $account_proxy;
$this->urlGenerator = $url_generator;
$this->logger = $logger;
$this->cache = $cache;
$this->time = $time;
$this->moduleHandler = $module_handler;
}
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('bynder_api'), $container
->get('current_user'), $container
->get('url_generator'), $container
->get('logger.factory'), $container
->get('cache.data'), $container
->get('datetime.time'), $container
->get('module_handler'));
}
public function getMetadataAttributes() {
$fields = [
'uuid' => $this
->t('ID'),
'name' => $this
->t('Name'),
'description' => $this
->t('Description'),
'tags' => $this
->t('Tags'),
'type' => $this
->t('Type'),
'video_preview_urls' => $this
->t('Video preview urls'),
'thumbnail_urls' => $this
->t('Thumbnail urls'),
'width' => $this
->t('Width'),
'height' => $this
->t('Height'),
'created' => $this
->t('Date created'),
'modified' => $this
->t('Data modified'),
'propertyOptions' => $this
->t('Meta-property option IDs'),
];
return $fields;
}
public function getRemoteMetadataProperties() {
return [
'id',
'name',
'description',
'type',
'videoPreviewURLs',
'thumbnails',
'original',
'width',
'height',
'dateCreated',
'dateModified',
'propertyOptions',
];
}
public function defaultConfiguration() {
return [
'source_field' => '',
];
}
public function ensureMetadata(MediaInterface $media, $force = FALSE) {
$media_uuid = $this
->getSourceFieldValue($media);
if (!empty($this->metadata[$media_uuid]) && !$force) {
return TRUE;
}
if (!$media
->hasField(BynderMetadataItem::METADATA_FIELD_NAME)) {
$this->logger
->get('bynder')
->error('The media type @type must have a Bynder metadata field named "bynder_metadata".', [
'@type' => $media
->bundle(),
]);
return FALSE;
}
if (!$media
->get(BynderMetadataItem::METADATA_FIELD_NAME)
->isEmpty() && !$force) {
$metadata = Json::decode($media
->get(BynderMetadataItem::METADATA_FIELD_NAME)->value);
if (is_array($metadata)) {
$this->metadata[$media_uuid] = $metadata;
return TRUE;
}
}
try {
if (!static::$timoutDetected || $force) {
$media_uuid = $this
->getSourceFieldValue($media);
$this->metadata[$media_uuid] = $this
->filterRemoteMetadata((array) $this->bynderApi
->getMediaInfo($media_uuid));
if ($this
->hasMetadataChanged($media, $this->metadata[$media_uuid])) {
$encoded_metadata = Json::encode($this->metadata[$media_uuid]);
if (!$encoded_metadata) {
$this->logger
->get('bynder')
->error('Unable to JSON encode the returned API response for the media UUID @uuid.', [
'@uuid' => $media_uuid,
]);
return FALSE;
}
$media
->set(BynderMetadataItem::METADATA_FIELD_NAME, $encoded_metadata)
->save();
return TRUE;
}
return TRUE;
}
} catch (GuzzleException $e) {
if ($e instanceof ConnectException) {
$handler_context = $e
->getHandlerContext();
if (isset($handler_context['errno']) && $handler_context['errno'] == 28) {
static::$timoutDetected = TRUE;
}
}
$this->logger
->get('bynder')
->error('Unable to fetch info about the asset represented by media @name (@id) with message @message.', [
'@name' => $media
->label(),
'@id' => $media
->id(),
'@message' => $e
->getMessage(),
]);
}
return FALSE;
}
public function filterRemoteMetadata(array $metadata) {
return array_intersect_key($metadata, array_combine($this
->getRemoteMetadataProperties(), $this
->getRemoteMetadataProperties()));
}
public function hasMetadataChanged(MediaInterface $media, array $remote_metadata) {
$remote_metadata = $this
->filterRemoteMetadata($remote_metadata);
if ($media
->get(BynderMetadataItem::METADATA_FIELD_NAME)
->isEmpty() && !empty($remote_metadata)) {
return TRUE;
}
$local_metadata = (array) Json::decode((string) $media
->get(BynderMetadataItem::METADATA_FIELD_NAME)->value);
return $local_metadata !== $remote_metadata;
}
public function getMetadata(MediaInterface $media, $name) {
$remote_uuid = $this
->getSourceFieldValue($media);
if ($name == 'uuid') {
return $remote_uuid;
}
if ($this
->ensureMetadata($media)) {
switch ($name) {
case 'video_preview_urls':
return isset($this->metadata[$remote_uuid]['videoPreviewURLs']) ? $this->metadata[$remote_uuid]['videoPreviewURLs'] : FALSE;
case 'thumbnail_urls':
return isset($this->metadata[$remote_uuid]['thumbnails']) ? $this->metadata[$remote_uuid]['thumbnails'] : FALSE;
case 'thumbnail_uri':
if (!empty($this->metadata[$remote_uuid]['thumbnails']['webimage'])) {
if ($this
->useRemoteImages()) {
return $this->metadata[$remote_uuid]['thumbnails']['webimage'];
}
elseif ($file = system_retrieve_file($this->metadata[$remote_uuid]['thumbnails']['webimage'], NULL, TRUE)) {
return $file
->getFileUri();
}
}
return parent::getMetadata($media, 'thumbnail_uri');
case 'created':
return isset($this->metadata[$remote_uuid]['dateCreated']) ? $this->metadata[$remote_uuid]['dateCreated'] : FALSE;
case 'modified':
return isset($this->metadata[$remote_uuid]['dateModified']) ? $this->metadata[$remote_uuid]['dateModified'] : FALSE;
case 'default_name':
return isset($this->metadata[$remote_uuid]['name']) ? $this->metadata[$remote_uuid]['name'] : parent::getMetadata($media, 'default_name');
default:
return isset($this->metadata[$remote_uuid][$name]) ? $this->metadata[$remote_uuid][$name] : FALSE;
}
}
return FALSE;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
try {
$this->bynderApi
->getBrands();
} catch (\Exception $exception) {
if ($this->accountProxy
->hasPermission('administer bynder configuration')) {
$this
->messenger()
->addError($this
->t('Connecting with Bynder failed. Check if the configuration is set properly <a href=":url">here</a>.', [
':url' => $this->urlGenerator
->generateFromRoute('bynder.configuration_form'),
]));
}
else {
$this
->messenger()
->addError($this
->t('Something went wrong with the Bynder connection. Please contact the site administrator.'));
}
}
return parent::buildConfigurationForm($form, $form_state);
}
public function getSourceFieldValue(MediaInterface $media) {
$source_field = $this->configuration['source_field'];
if (empty($source_field)) {
throw new \RuntimeException('Source field for media source is not defined.');
}
$field_item = $media
->get($source_field)
->first();
return $field_item->{$field_item
->mainPropertyName()};
}
public function createMetadataFieldStorage() {
return $this->entityTypeManager
->getStorage('field_storage_config')
->create([
'entity_type' => 'media',
'field_name' => BynderMetadataItem::METADATA_FIELD_NAME,
'type' => 'bynder_metadata',
'cardinality' => 1,
'locked' => TRUE,
]);
}
public function createMetadataField(MediaTypeInterface $type) {
return $this->entityTypeManager
->getStorage('field_config')
->create([
'entity_type' => 'media',
'field_name' => BynderMetadataItem::METADATA_FIELD_NAME,
'bundle' => $type
->id(),
'label' => 'Bynder Metadata',
'translatable' => FALSE,
'field_type' => 'bynder_metadata',
]);
}
protected function useRemoteImages() {
return $this->configFactory
->get('bynder.settings')
->get('use_remote_images') || $this->moduleHandler
->moduleExists('remote_stream_wrapper');
}
}