You are here

private function UsageEventSubscriber::getUsageEventMediainformation in Bynder 8.3

Same name and namespace in other branches
  1. 8.2 modules/bynder_usage/src/EventSubscriber/UsageEventSubscriber.php \Drupal\bynder_usage\EventSubscriber\UsageEventSubscriber::getUsageEventMediainformation()
  2. 4.0.x modules/bynder_usage/src/EventSubscriber/UsageEventSubscriber.php \Drupal\bynder_usage\EventSubscriber\UsageEventSubscriber::getUsageEventMediainformation()

Auxiliary function to get media information for asset usage operations.

Parameters

\Drupal\entity_usage\Events\EntityUsageEvent $event:

Return value

array|null

1 call to UsageEventSubscriber::getUsageEventMediainformation()
UsageEventSubscriber::onUsageRegister in modules/bynder_usage/src/EventSubscriber/UsageEventSubscriber.php
Triggers when a usage is registered (create or update) for a Bynder asset.

File

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

Class

UsageEventSubscriber
Listens for the usage events from Entity Usage module.

Namespace

Drupal\bynder_usage\EventSubscriber

Code

private function getUsageEventMediainformation(EntityUsageEvent $event) {
  if ($event
    ->getTargetEntityType() !== 'media') {
    return NULL;
  }

  /** @var \Drupal\media\MediaInterface $media */
  $media = $this->entityTypeManager
    ->getStorage('media')
    ->load($event
    ->getTargetEntityId());
  if (!isset($media)) {
    return NULL;
  }
  $source_plugin = $media
    ->getSource();
  if (!$source_plugin instanceof Bynder) {
    return NULL;
  }
  $url = NULL;
  if ($source_id = $event
    ->getSourceEntityId()) {
    if ($entity = $this->entityTypeManager
      ->getStorage($event
      ->getSourceEntityType())
      ->load($source_id)) {
      $url = $this
        ->getEntityUrl($entity);
    }
  }
  if ($url) {
    return [
      'mediaId' => $source_plugin
        ->getSourceFieldValue($media),
      'url' => $url,
    ];
  }
}