UsageEventSubscriber.php in Bynder 8.2
File
modules/bynder_usage/src/EventSubscriber/UsageEventSubscriber.php
View source
<?php
namespace Drupal\bynder_usage\EventSubscriber;
use Drupal\bynder\BynderApiInterface;
use Drupal\bynder\Plugin\media\Source\Bynder;
use Drupal\bynder_usage\Exception\UnableToAddUsageException;
use Drupal\bynder_usage\Exception\UnableToDeleteUsageException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
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;
}
$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());
while ($entity && $entity instanceof ParagraphInterface) {
$entity = $entity
->getParentEntity();
}
if ($entity && $entity
->hasLinkTemplate('canonical')) {
$url = $entity
->toUrl('canonical');
}
}
if ($url) {
return [
'mediaId' => $source_plugin
->getSourceFieldValue($media),
'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) {
watchdog_exception('bynder', $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) {
watchdog_exception('bynder', $e);
(new UnableToDeleteUsageException($e
->getMessage()))
->logException()
->displayMessage();
}
}
}
}