View source
<?php
namespace Drupal\bynder\Plugin\MediaEntity\Type;
use Drupal\bynder\BynderApiInterface;
use Drupal\Core\Config\Config;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\media_entity\MediaInterface;
use Drupal\media_entity\MediaTypeBase;
use GuzzleHttp\Exception\ConnectException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use GuzzleHttp\Exception\GuzzleException;
use Drupal\Core\Cache\CacheBackendInterface;
class Bynder extends MediaTypeBase {
protected $bynderApi;
protected $accountProxy;
protected $urlGenerator;
protected $apiResponse;
protected $logger;
protected $cache;
protected $configFactory;
protected $time;
protected static $timoutDetected = FALSE;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory, BynderApiInterface $bynder_api_service, AccountProxyInterface $account_proxy, UrlGeneratorInterface $url_generator, LoggerChannelFactoryInterface $logger, CacheBackendInterface $cache, TimeInterface $time) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $config_factory
->get('media_entity.settings'));
$this->bynderApi = $bynder_api_service;
$this->accountProxy = $account_proxy;
$this->urlGenerator = $url_generator;
$this->logger = $logger;
$this->cache = $cache;
$this->configFactory = $config_factory;
$this->time = $time;
}
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('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'));
}
public function providedFields() {
$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 defaultConfiguration() {
return [
'source_field' => '',
];
}
public function getField(MediaInterface $media, $name) {
if (!($source_field = $this->configuration['source_field'])) {
return FALSE;
}
if (!($media_uuid = $media->{$source_field}->value)) {
return FALSE;
}
if ($name == 'uuid') {
return $media_uuid;
}
if (!isset($this->apiResponse)) {
try {
if ($cache = $this->cache
->get('bynder_item_' . $media_uuid)) {
$this->apiResponse = $cache->data;
}
elseif (!static::$timoutDetected) {
$this->apiResponse = $this->bynderApi
->getMediaInfo($media_uuid);
$this->cache
->set('bynder_item_' . $media_uuid, $this->apiResponse, $this->time
->getRequestTime() + $this->configFactory
->get('bynder.settings')
->get('cache_lifetime'));
}
} 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;
}
}
if (!empty($this->apiResponse)) {
switch ($name) {
case 'video_preview_urls':
return isset($this->apiResponse['videoPreviewURLs']) ? $this->apiResponse['videoPreviewURLs'] : FALSE;
case 'thumbnail_urls':
return isset($this->apiResponse['thumbnails']) ? $this->apiResponse['thumbnails'] : FALSE;
case 'created':
return isset($this->apiResponse['dateCreated']) ? $this->apiResponse['dateCreated'] : FALSE;
case 'modified':
return isset($this->apiResponse['dateModified']) ? $this->apiResponse['dateModified'] : FALSE;
default:
return isset($this->apiResponse[$name]) ? $this->apiResponse[$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')) {
drupal_set_message($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'),
]), 'error');
}
else {
drupal_set_message($this
->t('Something went wrong with the Bynder connection. Please contact the site administrator.'), 'error');
}
}
$bundle = $form_state
->getFormObject()
->getEntity();
$options = [];
$allowed_field_types = [
'string',
'string_long',
];
foreach ($this->entityFieldManager
->getFieldDefinitions('media', $bundle
->id()) as $field_name => $field) {
if (in_array($field
->getType(), $allowed_field_types) && !$field
->getFieldStorageDefinition()
->isBaseField()) {
$options[$field_name] = $field
->getLabel();
}
}
$form['source_field'] = [
'#type' => 'select',
'#title' => $this
->t('Field with source information'),
'#description' => $this
->t('Field on media entity that stores the media UUID from Bynder. You can create a bundle without selecting a value for this dropdown initially. This dropdown can be populated after adding fields to the bundle.'),
'#default_value' => $this->configuration['source_field'] ?: NULL,
'#options' => $options,
];
return $form;
}
public function getDefaultThumbnail() {
return $this->config
->get('icon_base') . '/bynder_no_image.png';
}
public function thumbnail(MediaInterface $media) {
if ($thumbnail = $this
->getField($media, 'thumbnail_urls')) {
if (isset($thumbnail['webimage'])) {
if ($file = system_retrieve_file($thumbnail['webimage'], NULL, TRUE)) {
return $file
->getFileUri();
}
}
}
return $this
->getDefaultThumbnail();
}
public function getDefaultName(MediaInterface $media) {
if ($name = $this
->getField($media, 'name')) {
return $name;
}
return parent::getDefaultName($media);
}
}