WorkspaceManager.php in Workspace 8.2
File
src/WorkspaceManager.php
View source
<?php
namespace Drupal\workspace;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\State\StateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class WorkspaceManager implements WorkspaceManagerInterface {
use StringTranslationTrait;
protected $blacklist = [
'workspace_association',
'workspace',
];
protected $requestStack;
protected $entityTypeManager;
protected $currentUser;
protected $state;
protected $logger;
protected $classResolver;
protected $negotiatorIds;
protected $activeWorkspace;
public function __construct(RequestStack $request_stack, EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $current_user, StateInterface $state, LoggerInterface $logger, ClassResolverInterface $class_resolver, array $negotiator_ids) {
$this->requestStack = $request_stack;
$this->entityTypeManager = $entity_type_manager;
$this->currentUser = $current_user;
$this->state = $state;
$this->logger = $logger;
$this->classResolver = $class_resolver;
$this->negotiatorIds = $negotiator_ids;
}
public function isEntityTypeSupported(EntityTypeInterface $entity_type) {
if (!isset($this->blacklist[$entity_type
->id()]) && $entity_type
->entityClassImplements(EntityPublishedInterface::class) && $entity_type
->isRevisionable()) {
return TRUE;
}
$this->blacklist[$entity_type
->id()] = $entity_type
->id();
return FALSE;
}
public function getSupportedEntityTypes() {
$entity_types = [];
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $entity_type) {
if ($this
->isEntityTypeSupported($entity_type)) {
$entity_types[$entity_type_id] = $entity_type;
}
}
return $entity_types;
}
public function getActiveWorkspace() {
if (!isset($this->activeWorkspace)) {
$request = $this->requestStack
->getCurrentRequest();
foreach ($this->negotiatorIds as $negotiator_id) {
$negotiator = $this->classResolver
->getInstanceFromDefinition($negotiator_id);
if ($negotiator
->applies($request)) {
if ($this->activeWorkspace = $negotiator
->getActiveWorkspace($request)) {
break;
}
}
}
}
return $this->activeWorkspace;
}
public function setActiveWorkspace(WorkspaceInterface $workspace) {
if (!$workspace
->access('view') && !$workspace
->isDefaultWorkspace()) {
$this->logger
->error('Denied access to view workspace %workspace_label for user %uid', [
'%workspace_label' => $workspace
->label(),
'%uid' => $this->currentUser
->id(),
]);
throw new WorkspaceAccessException('The user does not have permission to view that workspace.');
}
$this->activeWorkspace = $workspace;
$request = $this->requestStack
->getCurrentRequest();
foreach ($this->negotiatorIds as $negotiator_id) {
$negotiator = $this->classResolver
->getInstanceFromDefinition($negotiator_id);
if ($negotiator
->applies($request)) {
$negotiator
->setActiveWorkspace($workspace);
break;
}
}
$supported_entity_types = $this
->getSupportedEntityTypes();
foreach ($supported_entity_types as $supported_entity_type) {
$this->entityTypeManager
->getStorage($supported_entity_type
->id())
->resetCache();
}
return $this;
}
public function shouldAlterOperations(EntityTypeInterface $entity_type) {
return $this
->isEntityTypeSupported($entity_type) && !$this
->getActiveWorkspace()
->isDefaultWorkspace();
}
public function purgeDeletedWorkspacesBatch() {
$deleted_workspace_ids = $this->state
->get('workspace.deleted', []);
if (empty($deleted_workspace_ids)) {
return;
}
$batch_size = Settings::get('entity_update_batch_size', 50);
$workspace_association_storage = $this->entityTypeManager
->getStorage('workspace_association');
$workspace_id = reset($deleted_workspace_ids);
$workspace_association_ids = $this
->getWorkspaceAssociationRevisionsToPurge($workspace_id, $batch_size);
if ($workspace_association_ids) {
$workspace_associations = $workspace_association_storage
->loadMultipleRevisions(array_keys($workspace_association_ids));
foreach ($workspace_associations as $workspace_association) {
$associated_entity_storage = $this->entityTypeManager
->getStorage($workspace_association->target_entity_type_id->value);
if ($entity = $associated_entity_storage
->loadRevision($workspace_association->target_entity_revision_id->value)) {
if ($entity
->isDefaultRevision()) {
$entity
->delete();
}
else {
$associated_entity_storage
->deleteRevision($workspace_association->target_entity_revision_id->value);
}
}
if ($workspace_association
->isDefaultRevision()) {
$workspace_association
->delete();
}
else {
$workspace_association_storage
->deleteRevision($workspace_association
->getRevisionId());
}
}
}
if (!$this
->getWorkspaceAssociationRevisionsToPurge($workspace_id, $batch_size)) {
unset($deleted_workspace_ids[$workspace_id]);
$this->state
->set('workspace.deleted', $deleted_workspace_ids);
}
}
protected function getWorkspaceAssociationRevisionsToPurge($workspace_id, $batch_size) {
return $this->entityTypeManager
->getStorage('workspace_association')
->getQuery()
->allRevisions()
->accessCheck(FALSE)
->condition('workspace', $workspace_id)
->sort('revision_id', 'ASC')
->range(0, $batch_size)
->execute();
}
}