ContentLockPreEntityDelete.php in Acquia Content Hub 8
File
acquia_contenthub_audit/src/EventSubscriber/ContentLockPreEntityDelete.php
View source
<?php
namespace Drupal\acquia_contenthub_audit\EventSubscriber;
use Drupal\acquia_contenthub_audit\AcquiaContentHubAuditEvents;
use Drupal\acquia_contenthub_audit\Event\AuditPreEntityDeleteEvent;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ContentLockPreEntityDelete implements EventSubscriberInterface {
protected $contentLock;
public function __construct(ModuleHandlerInterface $handler) {
if ($handler
->moduleExists('content_lock')) {
$this->contentLock = \Drupal::service('content_lock');
}
}
public static function getSubscribedEvents() {
$events[AcquiaContentHubAuditEvents::PRE_ENTITY_DELETE] = [
'onPreEntityDelete',
50,
];
return $events;
}
public function onPreEntityDelete(AuditPreEntityDeleteEvent $event) {
if (!$this->contentLock) {
return;
}
$entity = $event
->getEntity();
if ($this->contentLock
->isTranslationLockEnabled($entity
->getEntityTypeId())) {
$languages = $entity
->getTranslationLanguages();
foreach (array_keys($languages) as $langcode) {
$this->contentLock
->release($entity
->id(), $langcode, NULL, NULL, $entity
->getEntityTypeId());
}
}
else {
$this->contentLock
->release($entity
->id(), LanguageInterface::LANGCODE_NOT_SPECIFIED, NULL, NULL, $entity
->getEntityTypeId());
}
}
}