Verified Commit fdac75b8 authored by Alex Pott's avatar Alex Pott
Browse files

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

(cherry picked from commit f7950eb4)
parent c333e1f5
Loading
Loading
Loading
Loading
Loading
+27 −1
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\workspaces;

use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Query\PagerSelectExtender;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\RevisionableInterface;
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.
   *
   * 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
   */
@@ -188,6 +189,31 @@ public function getTrackedEntities($workspace_id, $entity_type_id = NULL, $entit
    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}
   */
+18 −0
Original line number Diff line number Diff line
@@ -53,6 +53,24 @@ public function workspaceInsert(WorkspaceInterface $workspace);
   */
  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.
   *
+18 −7
Original line number Diff line number Diff line
@@ -42,6 +42,11 @@ class WorkspaceViewBuilder extends EntityViewBuilder {
   */
  protected $bundleInfo;

  /**
   * The number of entities to display on the workspace manage page.
   */
  protected int|false $limit = 50;

  /**
   * {@inheritdoc}
   */
@@ -69,7 +74,13 @@ public function buildComponents(array &$build, array $entities, array $displays,
      'operations' => $this->t('Operations'),
    ];
    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());
      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'] = [
        '#type' => 'item',
@@ -82,13 +93,8 @@ public function buildComponents(array &$build, array $entities, array $displays,
        '#empty' => $this->t('This workspace has no changes.'),
      ];

      $changes_count = [];
      foreach ($all_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));

      $paged_tracked_entities = $this->workspaceAssociation->getTrackedEntitiesForListing($entity->id(), $build_id, $this->limit);
      foreach ($paged_tracked_entities as $entity_type_id => $tracked_entities) {
        $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
        if ($this->entityTypeManager->hasHandler($entity_type_id, 'list_builder')) {
          $list_builder = $this->entityTypeManager->getListBuilder($entity_type_id);
@@ -166,6 +172,11 @@ public function buildComponents(array &$build, array $entities, array $displays,
      if ($changes_count) {
        $build[$build_id]['changes']['overview']['#markup'] = implode(', ', $changes_count);
      }

      $build[$build_id]['pager'] = [
        '#type' => 'pager',
        '#element' => $build_id,
      ];
    }
  }

+17 −0
Original line number Diff line number Diff line
@@ -215,6 +215,23 @@ public function testWorkspaceManagePage() {
    $assert_session->linkExists('Node 1');
    $assert_session->linkExists('Node 2');
    $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');
  }

  /**