You are here

private function UsageEventSubscriber::getUsageEventMediainformation in Bynder 8.2

Same name and namespace in other branches
  1. 8.3 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

2 calls to UsageEventSubscriber::getUsageEventMediainformation()
UsageEventSubscriber::onAdd in modules/bynder_usage/src/EventSubscriber/UsageEventSubscriber.php
Adds usage for Bynder asset.
UsageEventSubscriber::onDelete in modules/bynder_usage/src/EventSubscriber/UsageEventSubscriber.php
Removes usage from Bynder asset.

File

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

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 ($event
    ->getReferencingEntityId()) {
    $entity = $this->entityTypeManager
      ->getStorage($event
      ->getReferencingEntityType())
      ->load($event
      ->getReferencingEntityId());

    // If the entity is a paragraph, attempt to recursively load the parent.
    while ($entity && $entity instanceof ParagraphInterface) {
      $entity = $entity
        ->getParentEntity();
    }

    // If the entity exists and has a canonical link template, get the URL.
    if ($entity && $entity
      ->hasLinkTemplate('canonical')) {
      $url = $entity
        ->toUrl('canonical');
    }
  }
  if ($url) {
    return [
      'mediaId' => $source_plugin
        ->getSourceFieldValue($media),
      'url' => $url,
    ];
  }
}