Issue #3273461 by amateescu, smustgrave, alexpott: Add pagination to the workspace manage page

(cherry picked from commit f7950eb418)
merge-requests/7853/head
Alex Pott 2024-04-30 12:35:53 +01:00
parent c333e1f5f4
commit fdac75b8b0
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
4 changed files with 80 additions and 8 deletions

View File

@ -3,6 +3,7 @@
namespace Drupal\workspaces; namespace Drupal\workspaces;
use Drupal\Core\Database\Connection; use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\PagerSelectExtender;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\RevisionableInterface; use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage; use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
@ -47,7 +48,7 @@ class WorkspaceAssociation implements WorkspaceAssociationInterface, EventSubscr
* A multidimensional array of entity IDs that are associated to a workspace. * A multidimensional array of entity IDs that are associated to a workspace.
* *
* The first level keys are workspace IDs, the second level keys are entity * The first level keys are workspace IDs, the second level keys are entity
* * type IDs, and the third level array are entity IDs, keyed by revision IDs. * type IDs, and the third level array are entity IDs, keyed by revision IDs.
* *
* @var array * @var array
*/ */
@ -188,6 +189,31 @@ class WorkspaceAssociation implements WorkspaceAssociationInterface, EventSubscr
return $tracked_revisions; return $tracked_revisions;
} }
/**
* {@inheritdoc}
*/
public function getTrackedEntitiesForListing($workspace_id, int $pager_id = NULL, int|false $limit = 50): array {
$query = $this->database->select(static::TABLE)
->extend(PagerSelectExtender::class)
->limit($limit);
if ($pager_id) {
$query->element($pager_id);
}
$query
->fields(static::TABLE, ['target_entity_type_id', 'target_entity_id', 'target_entity_revision_id'])
->orderBy('target_entity_type_id', 'ASC')
->orderBy('target_entity_revision_id', 'DESC')
->condition('workspace', $workspace_id);
$tracked_revisions = [];
foreach ($query->execute() as $record) {
$tracked_revisions[$record->target_entity_type_id][$record->target_entity_revision_id] = $record->target_entity_id;
}
return $tracked_revisions;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -53,6 +53,24 @@ interface WorkspaceAssociationInterface {
*/ */
public function getTrackedEntities($workspace_id, $entity_type_id = NULL, $entity_ids = NULL); public function getTrackedEntities($workspace_id, $entity_type_id = NULL, $entity_ids = NULL);
/**
* Retrieves a paged list of entities tracked by a given workspace.
*
* @param string $workspace_id
* The ID of the workspace.
* @param int|null $pager_id
* (optional) A pager ID. Defaults to NULL.
* @param int|false $limit
* (optional) An integer specifying the number of elements per page. If
* passed a false value (FALSE, 0, NULL), the pager is disabled. Defaults to
* 50.
*
* @return array
* Returns a multidimensional array where the first level keys are entity
* type IDs and the values are an array of entity IDs keyed by revision IDs.
*/
public function getTrackedEntitiesForListing($workspace_id, int $pager_id = NULL, int|false $limit = 50): array;
/** /**
* Retrieves all content revisions tracked by a given workspace. * Retrieves all content revisions tracked by a given workspace.
* *

View File

@ -42,6 +42,11 @@ class WorkspaceViewBuilder extends EntityViewBuilder {
*/ */
protected $bundleInfo; protected $bundleInfo;
/**
* The number of entities to display on the workspace manage page.
*/
protected int|false $limit = 50;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -69,7 +74,13 @@ class WorkspaceViewBuilder extends EntityViewBuilder {
'operations' => $this->t('Operations'), 'operations' => $this->t('Operations'),
]; ];
foreach ($entities as $build_id => $entity) { foreach ($entities as $build_id => $entity) {
// Display the number of entities changed in the workspace regardless of
// how many of them are listed on each page.
$changes_count = [];
$all_tracked_entities = $this->workspaceAssociation->getTrackedEntities($entity->id()); $all_tracked_entities = $this->workspaceAssociation->getTrackedEntities($entity->id());
foreach ($all_tracked_entities as $entity_type_id => $tracked_entity_ids) {
$changes_count[$entity_type_id] = $this->entityTypeManager->getDefinition($entity_type_id)->getCountLabel(count($tracked_entity_ids));
}
$build[$build_id]['changes']['overview'] = [ $build[$build_id]['changes']['overview'] = [
'#type' => 'item', '#type' => 'item',
@ -82,13 +93,8 @@ class WorkspaceViewBuilder extends EntityViewBuilder {
'#empty' => $this->t('This workspace has no changes.'), '#empty' => $this->t('This workspace has no changes.'),
]; ];
$changes_count = []; $paged_tracked_entities = $this->workspaceAssociation->getTrackedEntitiesForListing($entity->id(), $build_id, $this->limit);
foreach ($all_tracked_entities as $entity_type_id => $tracked_entities) { foreach ($paged_tracked_entities as $entity_type_id => $tracked_entities) {
// Ensure that newest revisions are displayed at the top.
krsort($tracked_entities);
$changes_count[$entity_type_id] = $this->entityTypeManager->getDefinition($entity_type_id)->getCountLabel(count($tracked_entities));
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id); $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
if ($this->entityTypeManager->hasHandler($entity_type_id, 'list_builder')) { if ($this->entityTypeManager->hasHandler($entity_type_id, 'list_builder')) {
$list_builder = $this->entityTypeManager->getListBuilder($entity_type_id); $list_builder = $this->entityTypeManager->getListBuilder($entity_type_id);
@ -166,6 +172,11 @@ class WorkspaceViewBuilder extends EntityViewBuilder {
if ($changes_count) { if ($changes_count) {
$build[$build_id]['changes']['overview']['#markup'] = implode(', ', $changes_count); $build[$build_id]['changes']['overview']['#markup'] = implode(', ', $changes_count);
} }
$build[$build_id]['pager'] = [
'#type' => 'pager',
'#element' => $build_id,
];
} }
} }

View File

@ -215,6 +215,23 @@ class WorkspaceTest extends BrowserTestBase {
$assert_session->linkExists('Node 1'); $assert_session->linkExists('Node 1');
$assert_session->linkExists('Node 2'); $assert_session->linkExists('Node 2');
$assert_session->linkExists('Term 1'); $assert_session->linkExists('Term 1');
// Create 50 more nodes to test the pagination.
for ($i = 3; $i < 53; $i++) {
$this->createNodeThroughUi('Node ' . $i, 'test');
}
$this->drupalGet($test_1->toUrl()->toString());
$assert_session->pageTextContains('52 content items');
$assert_session->pageTextContains('1 taxonomy term');
$assert_session->linkExists('Node 52');
$assert_session->linkExists('Node 3');
$assert_session->linkNotExists('Term 1');
$this->drupalGet($test_1->toUrl()->toString(), ['query' => ['page' => '1']]);
$assert_session->linkExists('Node 1');
$assert_session->linkExists('Node 2');
$assert_session->linkExists('Term 1');
} }
/** /**