You are here

class ContentLockPreEntityDelete in Acquia Content Hub 8

Content lock that occurs pre-entity delete.

@package Drupal\acquia_contenthub_audit\EventSubscriber

Hierarchy

  • class \Drupal\acquia_contenthub_audit\EventSubscriber\ContentLockPreEntityDelete implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ContentLockPreEntityDelete

1 string reference to 'ContentLockPreEntityDelete'
acquia_contenthub_audit.services.yml in acquia_contenthub_audit/acquia_contenthub_audit.services.yml
acquia_contenthub_audit/acquia_contenthub_audit.services.yml
1 service uses ContentLockPreEntityDelete
acquia_contenthub_audit.pre_entity_delete.content_lock in acquia_contenthub_audit/acquia_contenthub_audit.services.yml
Drupal\acquia_contenthub_audit\EventSubscriber\ContentLockPreEntityDelete

File

acquia_contenthub_audit/src/EventSubscriber/ContentLockPreEntityDelete.php, line 16

Namespace

Drupal\acquia_contenthub_audit\EventSubscriber
View source
class ContentLockPreEntityDelete implements EventSubscriberInterface {

  /**
   * The Content Lock Service.
   *
   * @var \Drupal\content_lock\ContentLock\ContentLock
   */
  protected $contentLock;

  /**
   * ContentLockPreEntityDelete constructor.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $handler
   *   The module handler.
   */
  public function __construct(ModuleHandlerInterface $handler) {
    if ($handler
      ->moduleExists('content_lock')) {
      $this->contentLock = \Drupal::service('content_lock');
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubAuditEvents::PRE_ENTITY_DELETE] = [
      'onPreEntityDelete',
      50,
    ];
    return $events;
  }

  /**
   * Deletes content locks on a particular entity.
   *
   * @param \Drupal\acquia_contenthub_audit\Event\AuditPreEntityDeleteEvent $event
   *   The audit pre entity delete event object.
   */
  public function onPreEntityDelete(AuditPreEntityDeleteEvent $event) {

    // If content_lock module is not enabled then exit inmediately.
    if (!$this->contentLock) {
      return;
    }
    $entity = $event
      ->getEntity();

    // If translation lock is enabled for this entity type, remove lock per
    // each language.
    if ($this->contentLock
      ->isTranslationLockEnabled($entity
      ->getEntityTypeId())) {
      $languages = $entity
        ->getTranslationLanguages();
      foreach (array_keys($languages) as $langcode) {

        // Release entity lock.
        $this->contentLock
          ->release($entity
          ->id(), $langcode, NULL, NULL, $entity
          ->getEntityTypeId());
      }
    }
    else {

      // Release entity lock.
      $this->contentLock
        ->release($entity
        ->id(), LanguageInterface::LANGCODE_NOT_SPECIFIED, NULL, NULL, $entity
        ->getEntityTypeId());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentLockPreEntityDelete::$contentLock protected property The Content Lock Service.
ContentLockPreEntityDelete::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ContentLockPreEntityDelete::onPreEntityDelete public function Deletes content locks on a particular entity.
ContentLockPreEntityDelete::__construct public function ContentLockPreEntityDelete constructor.