UsageEventSubscriber.php in Bynder 8
File
src/EventSubscriber/UsageEventSubscriber.php
View source
<?php
namespace Drupal\bynder\EventSubscriber;
use Drupal\bynder\BynderApiInterface;
use Drupal\bynder\Exception\UnableToAddUsageException;
use Drupal\bynder\Exception\UnableToDeleteUsageException;
use Drupal\bynder\Plugin\MediaEntity\Type\Bynder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Url;
use Drupal\entity_usage\Events\EntityUsageEvent;
use Drupal\entity_usage\Events\Events;
use Drupal\paragraphs\ParagraphInterface;
use GuzzleHttp\Exception\RequestException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class UsageEventSubscriber implements EventSubscriberInterface {
protected $bynderApi;
protected $entityTypeManager;
protected $requestStack;
public function __construct(BynderApiInterface $bynder_api_service, EntityTypeManagerInterface $entity_type_manager, RequestStack $request_stack) {
$this->bynderApi = $bynder_api_service;
$this->entityTypeManager = $entity_type_manager;
$this->requestStack = $request_stack;
}
public static function getSubscribedEvents() {
$events[Events::USAGE_ADD][] = [
'onAdd',
];
$events[Events::USAGE_DELETE][] = [
'onDelete',
];
return $events;
}
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());
while ($entity && $entity instanceof ParagraphInterface) {
$entity = $entity
->getParentEntity();
}
if ($entity && $entity
->hasLinkTemplate('canonical')) {
$url = $entity
->toUrl('canonical');
}
}
if ($url) {
return [
'mediaId' => $media->{$source_field}->value,
'url' => $url,
];
}
}
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) {
(new UnableToAddUsageException($e
->getMessage()))
->logException()
->displayMessage();
}
}
}
}
public function onDelete(EntityUsageEvent $event) {
$mediaInfo = $this
->getUsageEventMediainformation($event);
if (isset($mediaInfo['mediaId'])) {
try {
$this->bynderApi
->removeAssetUsage($mediaInfo['mediaId'], $mediaInfo['url']);
} catch (RequestException $e) {
(new UnableToDeleteUsageException($e
->getMessage()))
->logException()
->displayMessage();
}
}
}
}