Commit b9adf5d8 authored by catch's avatar catch

Issue #1798332 by jhedstrom, dawehner, vedpareek, prajaankit: Add paging to the EntityListBuilder

parent f9821ed8
......@@ -21,7 +21,8 @@ class ConfigEntityListBuilder extends EntityListBuilder {
* {@inheritdoc}
*/
public function load() {
$entities = $this->storage->loadMultipleOverrideFree();
$entity_ids = $this->getEntityIds();
$entities = $this->storage->loadMultipleOverrideFree($entity_ids);
// Sort the entities using the entity class's sort() method.
// See \Drupal\Core\Config\Entity\ConfigEntityBase::sort().
......
......@@ -39,6 +39,13 @@ class EntityListBuilder extends EntityHandlerBase implements EntityListBuilderIn
*/
protected $entityType;
/**
* The number of entities to list per page.
*
* @var int
*/
protected $limit = 50;
/**
* {@inheritdoc}
*/
......@@ -74,7 +81,21 @@ public function getStorage() {
* {@inheritdoc}
*/
public function load() {
return $this->storage->loadMultiple();
$entity_ids = $this->getEntityIds();
return $this->storage->loadMultiple($entity_ids);
}
/**
* Loads entity IDs using a pager.
*
* @return array
* An array of entity IDs.
*/
protected function getEntityIds() {
$query = $this->getStorage()->getQuery();
return $query
->pager($this->limit)
->execute();
}
/**
......@@ -189,7 +210,7 @@ public function buildOperations(EntityInterface $entity) {
* @todo Add a link to add a new item to the #empty text.
*/
public function render() {
$build = array(
$build['table'] = array(
'#type' => 'table',
'#header' => $this->buildHeader(),
'#title' => $this->getTitle(),
......@@ -198,9 +219,12 @@ public function render() {
);
foreach ($this->load() as $entity) {
if ($row = $this->buildRow($entity)) {
$build['#rows'][$entity->id()] = $row;
$build['table']['#rows'][$entity->id()] = $row;
}
}
$build['pager'] = array(
'#theme' => 'pager',
);
return $build;
}
......
......@@ -237,4 +237,34 @@ function testListUI() {
$this->assertText('There is no Test configuration yet.');
}
/**
* Test paging.
*/
public function testPager() {
$storage = \Drupal::entityManager()->getListBuilder('config_test')->getStorage();
// Create 51 test entities.
for ($i = 1; $i < 52; $i++) {
$storage->create(array(
'id' => str_pad($i, 2, '0', STR_PAD_LEFT),
'label' => 'Test config entity ' . $i,
'weight' => $i,
'protected_property' => $i,
))->save();
}
// Load the listing page.
$this->drupalGet('admin/structure/config_test');
// Item 51 should not be present.
$this->assertRaw('Test config entity 50', 'Config entity 50 is shown.');
$this->assertNoRaw('Test config entity 51', 'Config entity 51 is on the next page.');
// Browse to the next page.
$this->clickLink(t('Page 2'));
$this->assertNoRaw('Test config entity 50', 'Test config entity 50 is on the previous page.');
$this->assertRaw('dotted.default', 'Default config entity appears on page 2.');
$this->assertRaw('Test config entity 51', 'Test config entity 51 is on page 2.');
}
}
......@@ -31,10 +31,10 @@ protected function getFilterLabels() {
* {@inheritdoc}
*/
public function render() {
$table = parent::render();
$build = parent::render();
$filter = $this->getFilterLabels();
usort($table['#rows'], array($this, 'sortRows'));
usort($build['table']['#rows'], array($this, 'sortRows'));
$build['filters'] = array(
'#type' => 'container',
......@@ -56,7 +56,6 @@ public function render() {
),
);
$build['table'] = $table;
$build['table']['#attributes']['class'][] = 'config-translation-entity-list';
$build['#attached']['library'][] = 'system/drupal.system.modules';
......
......@@ -83,8 +83,8 @@ public function render($target_entity_type_id = NULL, $target_bundle = NULL) {
$this->targetBundle = $target_bundle;
$build = parent::render();
$build['#attributes']['id'] = 'field-overview';
$build['#empty'] = $this->t('No fields are present yet.');
$build['table']['#attributes']['id'] = 'field-overview';
$build['table']['#empty'] = $this->t('No fields are present yet.');
return $build;
}
......
<?php
/**
* @file
* Contains \Drupal\system\Tests\Entity\EntityListBuilderTest.
*/
namespace Drupal\system\Tests\Entity;
use Drupal\simpletest\WebTestBase;
/**
* Tests entity list builder functionality.
*
* @group Entity
*/
class EntityListBuilderTest extends WebTestBase {
/**
* {@inheritdoc}
*/
public static $modules = array('entity_test');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Create and login user.
$this->web_user = $this->drupalCreateUser(array(
'administer entity_test content',
));
$this->drupalLogin($this->web_user);
}
/**
* Test paging.
*/
public function testPager() {
// Create 51 test entities.
for ($i = 1; $i < 52; $i++) {
entity_create('entity_test', array('name' => 'Test entity ' . $i))->save();
}
// Load the listing page.
$this->drupalGet('entity_test/list');
// Item 51 should not be present.
$this->assertRaw('Test entity 50', 'Item 50 is shown.');
$this->assertNoRaw('Test entity 51', 'Item 51 is on the next page.');
// Browse to the next page.
$this->clickLink(t('Page 2'));
$this->assertNoRaw('Test entity 50', 'Test entity 50 is on the previous page.');
$this->assertRaw('Test entity 51', 'Test entity 51 is shown.');
}
}
......@@ -48,6 +48,13 @@ entity.entity_test.collection_empty:
requirements:
_access: 'TRUE'
entity.entity_test.collection:
path: '/entity_test/list'
defaults:
_entity_list: entity_test
requirements:
_access: 'TRUE'
entity.block.test_operation:
path: '/admin/structure/block/manage/{block}/test_operation'
defaults:
......
......@@ -82,7 +82,7 @@ public function render() {
unset($this->weightKey);
}
$build = parent::render();
$build['#empty'] = t('No vocabularies available. <a href="@link">Add vocabulary</a>.', array('@link' => \Drupal::url('entity.taxonomy_vocabulary.add_form')));
$build['table']['#empty'] = t('No vocabularies available. <a href="@link">Add vocabulary</a>.', array('@link' => \Drupal::url('entity.taxonomy_vocabulary.add_form')));
return $build;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment