Skip to content
Snippets Groups Projects

Reapply "Issue #3473358: Some cleanup and D10 compatibility"

Open Devin Zuczek requested to merge issue/workspaces_parallel-3473358:3473358-reapply into 1.0.x
3 files
+ 66
17
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -2,15 +2,36 @@
@@ -2,15 +2,36 @@
namespace Drupal\workspaces_parallel\Service;
namespace Drupal\workspaces_parallel\Service;
 
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityRepository;
use Drupal\Core\Entity\EntityInterface;
 
use Drupal\Core\Entity\EntityRepositoryInterface;
 
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Entity\RevisionableInterface;
 
use Drupal\workspaces_parallel\WorkspacesParallelServiceProvider;
 
use Symfony\Component\DependencyInjection\Attribute\AutowireServiceClosure;
/**
/**
* Custom entity repository to deliver the most recent revisions, taking
* Custom entity repository to deliver the most recent revisions, taking
* workspaces into account.
* workspaces into account.
*/
*/
class ParallelWorkspaceEntityRepository extends EntityRepository {
class ParallelWorkspaceEntityRepository implements EntityRepositoryInterface {
 
 
public function __construct(
 
protected EntityRepositoryInterface $entityRepository,
 
protected EntityTypeManagerInterface $entityTypeManager,
 
protected Connection $connection,
 
// It's impossible to know whether these services use the entity
 
// repository or not and so inject these as closures to avoid any circular
 
// dependencies.
 
#[AutowireServiceClosure(WorkspacesParallelServiceProvider::WP_WORKSPACES_INFORMATION)]
 
protected \Closure $workspacesInformationClosure,
 
#[AutowireServiceClosure('workspaces.association')]
 
protected \Closure $workspacesAssociationClosure,
 
#[AutowireServiceClosure('workspaces.manager')]
 
protected \Closure $workspacesManagerClosure,
 
) {
 
}
/**
/**
* Get the latest revision for editing.
* Get the latest revision for editing.
@@ -25,11 +46,11 @@ class ParallelWorkspaceEntityRepository extends EntityRepository {
@@ -25,11 +46,11 @@ class ParallelWorkspaceEntityRepository extends EntityRepository {
*/
*/
public function getActiveMultiple($entity_type_id, array $entity_ids, array $contexts = NULL) {
public function getActiveMultiple($entity_type_id, array $entity_ids, array $contexts = NULL) {
/** @var \Drupal\workspaces\WorkspaceInformationInterface $workspaceInformation */
/** @var \Drupal\workspaces\WorkspaceInformationInterface $workspaceInformation */
$workspaceInformation = \Drupal::service('workspaces.information');
$workspaceInformation = ($this->workspacesInformationClosure)();
if (!$workspaceInformation->isEntityTypeSupported($this->entityTypeManager->getDefinition($entity_type_id))) {
if (!$workspaceInformation->isEntityTypeSupported($this->entityTypeManager->getDefinition($entity_type_id))) {
// Entity not supported by Workspaces.
// Entity not supported by Workspaces.
return parent::getActiveMultiple($entity_type_id, $entity_ids, $contexts);
return $this->entityRepository->getActiveMultiple($entity_type_id, $entity_ids, $contexts);
}
}
$active = [];
$active = [];
@@ -56,19 +77,17 @@ class ParallelWorkspaceEntityRepository extends EntityRepository {
@@ -56,19 +77,17 @@ class ParallelWorkspaceEntityRepository extends EntityRepository {
* A workspace-supported entity.
* A workspace-supported entity.
*/
*/
private function getLatestWorkspaceAwareRevision(RevisionableInterface $entity) {
private function getLatestWorkspaceAwareRevision(RevisionableInterface $entity) {
$entityTypeManager = \Drupal::entityTypeManager();
/** @var \Drupal\workspaces\WorkspaceManagerInterface $workspaceManager */
/** @var \Drupal\workspaces\WorkspaceManagerInterface $workspaceManager */
$workspaceManager = \Drupal::service('workspaces.manager');
$workspaceManager = ($this->workspacesManagerClosure)();
/** @var \Drupal\workspaces\WorkspaceAssociationInterface $workspaceAssociation */
/** @var \Drupal\workspaces\WorkspaceAssociationInterface $workspaceAssociation */
$workspaceAssociation = \Drupal::service('workspaces.association');
$workspaceAssociation = ($this->workspacesAssociationClosure)();
// Get a list of revision IDs for entities that have a revision set for the
// Get a list of revision IDs for entities that have a revision set for the
// current active workspace. If an entity has multiple revisions set for a
// current active workspace. If an entity has multiple revisions set for a
// workspace, only the one with the highest ID is returned.
// workspace, only the one with the highest ID is returned.
if ($workspaceManager->getActiveWorkspace() && $tracked_entities = $workspaceAssociation->getTrackedEntities($workspaceManager->getActiveWorkspace()->id(), $entity->getEntityTypeId(), [$entity->id()])) {
if ($workspaceManager->getActiveWorkspace() && $tracked_entities = $workspaceAssociation->getTrackedEntities($workspaceManager->getActiveWorkspace()->id(), $entity->getEntityTypeId(), [$entity->id()])) {
/** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
/** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
$storage = $entityTypeManager->getStorage($entity->getEntityTypeId());
$storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
$vid = key($tracked_entities[$entity->getEntityTypeId()]);
$vid = key($tracked_entities[$entity->getEntityTypeId()]);
@@ -85,7 +104,7 @@ class ParallelWorkspaceEntityRepository extends EntityRepository {
@@ -85,7 +104,7 @@ class ParallelWorkspaceEntityRepository extends EntityRepository {
* Get the latest revision of an entity not associated with a workspace.
* Get the latest revision of an entity not associated with a workspace.
*/
*/
private function getLatestNonWorkspaceRevision(RevisionableInterface $entity) {
private function getLatestNonWorkspaceRevision(RevisionableInterface $entity) {
$storage = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId());
$storage = $this->entityTypeManager->getStorage($entity->getEntityTypeId());
/** @var ContentEntityTypeInterface $entityType */
/** @var ContentEntityTypeInterface $entityType */
$entityType = $entity->getEntityType();
$entityType = $entity->getEntityType();
@@ -93,7 +112,7 @@ class ParallelWorkspaceEntityRepository extends EntityRepository {
@@ -93,7 +112,7 @@ class ParallelWorkspaceEntityRepository extends EntityRepository {
$revisionMetadataKey = $entityType->getKey('revision');
$revisionMetadataKey = $entityType->getKey('revision');
$entityIdKey = $entityType->getKey('id');
$entityIdKey = $entityType->getKey('id');
$query = \Drupal::database()->select($revisionTable);
$query = $this->connection->select($revisionTable);
$query
$query
->fields($revisionTable, [$revisionMetadataKey])
->fields($revisionTable, [$revisionMetadataKey])
->orderBy($revisionMetadataKey, 'DESC')
->orderBy($revisionMetadataKey, 'DESC')
@@ -112,4 +131,28 @@ class ParallelWorkspaceEntityRepository extends EntityRepository {
@@ -112,4 +131,28 @@ class ParallelWorkspaceEntityRepository extends EntityRepository {
}
}
}
}
 
public function loadEntityByUuid($entity_type_id, $uuid) {
 
return $this->entityRepository->loadEntityByUuid($entity_type_id, $uuid);
 
}
 
 
public function loadEntityByConfigTarget($entity_type_id, $target) {
 
return $this->entityRepository->loadEntityByConfigTarget($entity_type_id, $target);
 
}
 
 
public function getTranslationFromContext(EntityInterface $entity, $langcode = NULL, $context = []) {
 
return $this->entityRepository->getTranslationFromContext($entity, $langcode, $context);
 
}
 
 
public function getActive($entity_type_id, $entity_id, array $contexts = NULL) {
 
return $this->entityRepository->getActive($entity_type_id, $entity_id, $contexts);
 
}
 
 
public function getCanonical($entity_type_id, $entity_id, array $contexts = NULL) {
 
return $this->entityRepository->getCanonical($entity_type_id, $entity_id, $contexts);
 
}
 
 
public function getCanonicalMultiple($entity_type_id, array $entity_ids, array $contexts = NULL) {
 
return $this->entityRepository->getCanonicalMultiple($entity_type_id, $entity_ids, $contexts);
 
}
 
}
}
Loading