You are here

private function UsageEventSubscriber::getUsageEventMediainformation in Bynder 8

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 src/EventSubscriber/UsageEventSubscriber.php
Adds usage for Bynder asset.
UsageEventSubscriber::onDelete in src/EventSubscriber/UsageEventSubscriber.php
Removes usage from Bynder asset.

File

src/EventSubscriber/UsageEventSubscriber.php, line 77

Class

UsageEventSubscriber
Listens for the usage events from Entity Usage module.

Namespace

Drupal\bynder\EventSubscriber

Code

private function getUsageEventMediainformation(EntityUsageEvent $event) {
  if ($event
    ->getTargetEntityType() !== 'media') {
    return NULL;
  }
  $media = $this->entityTypeManager
    ->getStorage('media')
    ->load($event
    ->getTargetEntityId());
  if (!isset($media)) {
    return NULL;
  }
  $bundle = $this->entityTypeManager
    ->getStorage('media_bundle')
    ->load($media
    ->bundle());
  if (!$bundle
    ->getType() instanceof Bynder) {
    return NULL;
  }
  $source_field = $bundle
    ->getTypeConfiguration()['source_field'];
  $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' => $media->{$source_field}->value,
      'url' => $url,
    ];
  }
}