You are here

public function UsageEventSubscriber::onAdd in Bynder 8.2

Adds usage for Bynder asset.

Parameters

\Drupal\entity_usage\Events\EntityUsageEvent $event: The event to process.

File

modules/bynder_usage/src/EventSubscriber/UsageEventSubscriber.php, line 122

Class

UsageEventSubscriber
Listens for the usage events from Entity Usage module.

Namespace

Drupal\bynder_usage\EventSubscriber

Code

public function onAdd(EntityUsageEvent $event) {
  if (\Drupal::service('module_handler')
    ->moduleExists('entity_usage')) {
    $mediaInfo = $this
      ->getUsageEventMediainformation($event);
    if (isset($mediaInfo)) {
      try {
        $this->bynderApi
          ->addAssetUsage($mediaInfo['mediaId'], $mediaInfo['url'], date(DATE_ISO8601, \Drupal::time()
          ->getRequestTime()), 'Added asset by user ' . \Drupal::currentUser()
          ->getAccountName() . '.');
      } catch (RequestException $e) {
        watchdog_exception('bynder', $e);
        (new UnableToAddUsageException($e
          ->getMessage()))
          ->logException()
          ->displayMessage();
      }
    }
  }
}