You are here

public function Bynder::getField in Bynder 8

Gets a media-related field/value.

Parameters

MediaInterface $media: Media object.

string $name: Name of field to fetch.

Return value

mixed Field value or FALSE if data unavailable.

Overrides MediaTypeInterface::getField

2 calls to Bynder::getField()
Bynder::getDefaultName in src/Plugin/MediaEntity/Type/Bynder.php
Provide a default name for the media.
Bynder::thumbnail in src/Plugin/MediaEntity/Type/Bynder.php
Gets thumbnail image.

File

src/Plugin/MediaEntity/Type/Bynder.php, line 189

Class

Bynder
Provides media type plugin for Bynder.

Namespace

Drupal\bynder\Plugin\MediaEntity\Type

Code

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;
}