You are here

Bynder.php in Bynder 8

File

src/Plugin/MediaEntity/Type/Bynder.php
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;

/**
 * Provides media type plugin for Bynder.
 *
 * @MediaType(
 *   id = "bynder",
 *   label = @Translation("Bynder"),
 *   description = @Translation("Provides business logic and metadata for Bynder.")
 * )
 */
class Bynder extends MediaTypeBase {

  /**
   * Bynder api service.
   *
   * @var \Drupal\bynder\BynderApiInterface
   *   Bynder api service.
   */
  protected $bynderApi;

  /**
   * Account proxy.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $accountProxy;

  /**
   * The url generator.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface
   */
  protected $urlGenerator;

  /**
   * Statically cached API response for a given asset.
   *
   * @var array
   */
  protected $apiResponse;

  /**
   * The logger factory service.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
   */
  protected $logger;

  /**
   * The cache service.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * Used to track if an API request returned a timout error.
   *
   * @var bool
   */
  protected static $timoutDetected = FALSE;

  /**
   * Constructs a new class instance.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager service.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   Entity field manager service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The Config factory.
   * @param \Drupal\bynder\BynderApiInterface $bynder_api_service
   *   Bynder api service.
   * @param \Drupal\Core\Session\AccountProxyInterface $account_proxy
   *   Account proxy.
   * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
   *   The url generator service.
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger
   *   The logger factory service.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   The cache service.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'source_field' => '',
    ];
  }

  /**
   * {@inheritdoc}
   */
  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 {

        // Check for cached data first, either set below or by
        // \Drupal\bynder\Plugin\EntityBrowser\Widget\BynderSearch::submit(),
        // to avoid extra API requests.
        if ($cache = $this->cache
          ->get('bynder_item_' . $media_uuid)) {
          $this->apiResponse = $cache->data;
        }
        elseif (!static::$timoutDetected) {
          $this->apiResponse = $this->bynderApi
            ->getMediaInfo($media_uuid);

          // Cache the response for the configured timeframe.
          $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;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {

    // Check the connection with bynder.
    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');
      }
    }

    /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
    $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;
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultThumbnail() {
    return $this->config
      ->get('icon_base') . '/bynder_no_image.png';
  }

  /**
   * {@inheritdoc}
   */
  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();
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultName(MediaInterface $media) {
    if ($name = $this
      ->getField($media, 'name')) {
      return $name;
    }
    return parent::getDefaultName($media);
  }

}

Classes

Namesort descending Description
Bynder Provides media type plugin for Bynder.