class UsageEventSubscriber in Bynder 8.2
Same name and namespace in other branches
- 8.3 modules/bynder_usage/src/EventSubscriber/UsageEventSubscriber.php \Drupal\bynder_usage\EventSubscriber\UsageEventSubscriber
- 4.0.x modules/bynder_usage/src/EventSubscriber/UsageEventSubscriber.php \Drupal\bynder_usage\EventSubscriber\UsageEventSubscriber
Listens for the usage events from Entity Usage module.
Hierarchy
- class \Drupal\bynder_usage\EventSubscriber\UsageEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of UsageEventSubscriber
1 string reference to 'UsageEventSubscriber'
- bynder_usage.services.yml in modules/
bynder_usage/ bynder_usage.services.yml - modules/bynder_usage/bynder_usage.services.yml
1 service uses UsageEventSubscriber
- bynder_usage_subscriber in modules/
bynder_usage/ bynder_usage.services.yml - Drupal\bynder_usage\EventSubscriber\UsageEventSubscriber
File
- modules/
bynder_usage/ src/ EventSubscriber/ UsageEventSubscriber.php, line 20
Namespace
Drupal\bynder_usage\EventSubscriberView source
class UsageEventSubscriber implements EventSubscriberInterface {
/**
* Bynder api service.
*
* @var \Drupal\bynder\BynderApiInterface
* Bynder api service.
*/
protected $bynderApi;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* UsageEventSubscriber constructor.
*
* @param \Drupal\bynder\BynderApiInterface $bynder_api_service
* Bynder api service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
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;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[Events::USAGE_ADD][] = [
'onAdd',
];
$events[Events::USAGE_DELETE][] = [
'onDelete',
];
return $events;
}
/**
* Auxiliary function to get media information for asset usage operations.
*
* @param \Drupal\entity_usage\Events\EntityUsageEvent $event
*
* @return array|null
*/
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,
];
}
}
/**
* Adds usage for Bynder asset.
*
* @param \Drupal\entity_usage\Events\EntityUsageEvent $event
* The event to process.
*/
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();
}
}
}
}
/**
* Removes usage from Bynder asset.
*
* @param \Drupal\entity_usage\Events\EntityUsageEvent $event
* The event to process.
*/
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();
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
UsageEventSubscriber:: |
protected | property | Bynder api service. | |
UsageEventSubscriber:: |
protected | property | The entity type manager. | |
UsageEventSubscriber:: |
protected | property | The request stack. | |
UsageEventSubscriber:: |
public static | function | ||
UsageEventSubscriber:: |
private | function | Auxiliary function to get media information for asset usage operations. | |
UsageEventSubscriber:: |
public | function | Adds usage for Bynder asset. | |
UsageEventSubscriber:: |
public | function | Removes usage from Bynder asset. | |
UsageEventSubscriber:: |
public | function | UsageEventSubscriber constructor. |