Skip to content

Commit 7a440e3

Browse files
authored
Merge pull request #61 from ahmed-bhs/feature/provide-pagination-infinite-scroll
Add traditional pagination to both admin bridges
2 parents 8789d9d + 58b13d2 commit 7a440e3

File tree

34 files changed

+1392
-16
lines changed

34 files changed

+1392
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- feature - added pagination to admin bridges media lists
6+
37
## [0.1.4] - 2024-11-23
48

59
- fix - do not trigger an error when no library is defined

demo/application/config/packages/joli_media_easy_admin.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
joli_media_easy_admin:
2+
pagination:
3+
per_page: 20
24
visibility:
35
show_variations_list: true
46
show_variations_list_admin_variations: true

demo/application/config/packages/joli_media_sonata_admin.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
joli_media_sonata_admin:
2+
pagination:
3+
per_page: 25
24
visibility:
35
show_variations_list: true
46
show_variations_list_admin_variations: true

demo/application/src/DataFixtures/MediaFixtures.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ class MediaFixtures extends Fixture
1111
#[\Override]
1212
public function load(ObjectManager $manager): void
1313
{
14+
$mediaRoot = __DIR__ . '/../../public/media/original';
1415
$filesystem = new Filesystem();
15-
$filesystem->mirror(
16-
__DIR__ . '/media',
17-
__DIR__ . '/../../public/media/original'
18-
);
16+
$filesystem->mirror(__DIR__ . '/media', $mediaRoot);
17+
18+
$filesystem->mkdir($mediaRoot . '/many-files');
19+
20+
for ($i = 0; $i < 100; ++$i) {
21+
$filesystem->copy(
22+
__DIR__ . '/media/articles/circle-pattern.png',
23+
$mediaRoot . '/many-files/circle-pattern-' . $i . '.png',
24+
);
25+
}
1926
}
2027
}

demo/application/src/DataFixtures/PostFixtures.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function load(ObjectManager $manager): void
2828
{
2929
$extensions = ['avif', 'gif', 'jpeg', 'png', 'webp'];
3030

31-
for ($i = 0; $i < 10; ++$i) {
31+
for ($i = 0; $i < 100; ++$i) {
3232
$extension = $extensions[$this->faker->numberBetween(0, \count($extensions) - 1)];
3333
$media = $this->resolver->resolve('articles/circle-pattern.' . $extension);
3434
$paragraphs = [];

doc/bridges/easy-admin.rst

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ The integration can be configured in the ``config/packages/joli_media_easy_admin
3737
.. code-block:: yaml
3838
3939
joli_media_easy_admin:
40+
pagination:
41+
per_page: 20
4042
upload:
4143
max_files: 10
4244
max_file_size: 20
@@ -53,6 +55,10 @@ The integration can be configured in the ``config/packages/joli_media_easy_admin
5355
Configuration Options
5456
~~~~~~~~~~~~~~~~~~~~~
5557

58+
The ``pagination`` section controls how media items are loaded and displayed:
59+
60+
- ``per_page``: Number of media items to display per page (default: ``20``). This improves performance for large libraries by loading only a subset of items.
61+
5662
The ``upload`` section of the configuration allows you to control the media upload behavior in EasyAdmin:
5763

5864
- ``max_files``: Sets the maximum number of files that can be uploaded at once.
@@ -66,8 +72,21 @@ The ``visibility`` section of the configuration allows you to control the visibi
6672
- ``show_html_code``: Displays the HTML code for embedding media.
6773
- ``show_markdown_code``: Displays the Markdown code for embedding media.
6874

69-
Media library menu item
70-
-----------------------
75+
Pagination and Performance
76+
~~~~~~~~~~~~~~~~~~~~~~~~~~
77+
78+
For large media libraries (hundreds or thousands of files), pagination significantly improves performance by loading only a subset of items at a time. The media library uses traditional page navigation with Previous/Next buttons, which is ideal for precise navigation in very large libraries.
79+
80+
You can configure the number of items displayed per page:
81+
82+
.. code-block:: yaml
83+
84+
joli_media_easy_admin:
85+
pagination:
86+
per_page: 20
87+
88+
Media libray menu item
89+
----------------------
7190

7291
To add a link to the media library in your EasyAdmin menu, you need to use the ``MenuItem::linkToRoute`` method, with the ``joli_media_easy_admin_explore`` route::
7392

doc/bridges/sonata-admin.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ The integration can be configured in the ``config/packages/joli_media_sonata_adm
3737
.. code-block:: yaml
3838
3939
joli_media_sonata_admin:
40+
pagination:
41+
per_page: 25
42+
upload:
43+
max_files: 10
44+
max_file_size: 20
45+
accepted_files:
46+
- image/*
4047
visibility:
4148
show_variations_stored: true
4249
show_variations_action_regenerate: true
@@ -46,6 +53,10 @@ The integration can be configured in the ``config/packages/joli_media_sonata_adm
4653
Configuration Options
4754
~~~~~~~~~~~~~~~~~~~~~
4855

56+
The ``pagination`` section controls how media items are loaded and displayed:
57+
58+
- ``per_page``: Number of media items to display per page (default: ``25``). This improves performance for large libraries by loading only a subset of items.
59+
4960
The ``upload`` section of the configuration allows you to control the media upload behavior in Sonata admin:
5061

5162
- ``max_files``: Sets the maximum number of files that can be uploaded at once.
@@ -59,6 +70,19 @@ The ``visibility`` section of the configuration allows you to control the visibi
5970
- ``show_html_code``: Displays the HTML code for embedding media.
6071
- ``show_markdown_code``: Displays the Markdown code for embedding media.
6172

73+
Pagination and Performance
74+
~~~~~~~~~~~~~~~~~~~~~~~~~~
75+
76+
For large media libraries (hundreds or thousands of files), pagination significantly improves performance by loading only a subset of items at a time. The media library uses traditional page navigation with Previous/Next buttons, which is ideal for precise navigation in very large libraries.
77+
78+
You can configure the number of items displayed per page:
79+
80+
.. code-block:: yaml
81+
82+
joli_media_sonata_admin:
83+
pagination:
84+
per_page: 25
85+
6286
Media selector widget
6387
---------------------
6488

src/Bridge/EasyAdmin/config/services.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use JoliCode\MediaBundle\Bridge\EasyAdmin\Form\DataTransformer\MediaTransformer;
99
use JoliCode\MediaBundle\Bridge\EasyAdmin\Form\Type\MediaChoiceType;
1010
use JoliCode\MediaBundle\Bridge\EasyAdmin\Form\Type\UploadType;
11+
use JoliCode\MediaBundle\Bridge\EasyAdmin\Paginator\MediaPaginator;
1112
use JoliCode\MediaBundle\Bridge\Security\Voter\MediaVoter;
1213
use JoliCode\MediaBundle\Bridge\Twig\JoliMediaAdminExtension;
1314

@@ -24,6 +25,12 @@
2425
'$translator' => service('translator')->ignoreOnInvalid(),
2526
])
2627

28+
// paginator
29+
->set('joli_media_easy_admin.paginator', MediaPaginator::class)
30+
->args([
31+
'$adminUrlGenerator' => service(AdminUrlGenerator::class),
32+
])
33+
2734
// controller
2835
->set('joli_media_easy_admin.controller.admin', MediaAdminController::class)
2936
->args([
@@ -35,6 +42,7 @@
3542
'$twig' => service('twig'),
3643
'$formFactory' => service('form.factory'),
3744
'$adminUrlGenerator' => service(AdminUrlGenerator::class),
45+
'$mediaPaginator' => service('joli_media_easy_admin.paginator'),
3846
'$authorizationChecker' => service('security.authorization_checker')->ignoreOnInvalid(),
3947
])
4048
->call('setContainer', [service('service_container')])

src/Bridge/EasyAdmin/src/Config/Config.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function __construct(
1818
private array $acceptedFiles,
1919
private int $maxFileSize,
2020
private ?int $maxFiles = null,
21+
private int $paginationSize = 20,
2122
) {
2223
}
2324

@@ -61,4 +62,9 @@ public function getUploadOptions(): array
6162

6263
return $config;
6364
}
65+
66+
public function getPaginationSize(): int
67+
{
68+
return $this->paginationSize;
69+
}
6470
}

src/Bridge/EasyAdmin/src/Controller/MediaAdminController.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use JoliCode\MediaBundle\Bridge\EasyAdmin\Form\Type\RenameDirectoryType;
1414
use JoliCode\MediaBundle\Bridge\EasyAdmin\Form\Type\RenameType;
1515
use JoliCode\MediaBundle\Bridge\EasyAdmin\Form\Type\UploadType;
16+
use JoliCode\MediaBundle\Bridge\EasyAdmin\Paginator\MediaPaginator;
1617
use JoliCode\MediaBundle\Bridge\Security\Voter\AdminAction;
1718
use JoliCode\MediaBundle\Conversion\Converter;
1819
use JoliCode\MediaBundle\Exception\ForbiddenPathException;
@@ -30,6 +31,7 @@
3031
use Symfony\Component\Asset\VersionStrategy\JsonManifestVersionStrategy;
3132
use Symfony\Component\Form\FormFactoryInterface;
3233
use Symfony\Component\Form\FormInterface;
34+
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
3335
use Symfony\Component\HttpFoundation\File\Exception\UploadException;
3436
use Symfony\Component\HttpFoundation\File\UploadedFile;
3537
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -53,6 +55,7 @@ public function __construct(
5355
private readonly Environment $twig,
5456
private readonly FormFactoryInterface $formFactory,
5557
private readonly AdminUrlGenerator $adminUrlGenerator,
58+
private readonly MediaPaginator $mediaPaginator,
5659
private readonly ?AuthorizationCheckerInterface $authorizationChecker = null,
5760
) {
5861
}
@@ -298,6 +301,19 @@ public function list(AdminContext $adminContext, Request $request, string $key =
298301
default => 'explore',
299302
};
300303

304+
try {
305+
$paginatedMedias = $this->getOriginalStorage()->listMediasPaginated(
306+
$currentKey,
307+
recursive: false,
308+
page: $request->query->getInt('page', 1),
309+
perPage: $this->config->getPaginationSize(),
310+
);
311+
} catch (\OutOfRangeException) {
312+
throw new BadRequestException('The requested page number is out of range.');
313+
}
314+
315+
$paginator = $this->mediaPaginator->paginate($paginatedMedias, $routeName, $currentKey);
316+
301317
return new Response($this->twig->render('@JoliMediaEasyAdmin/list.html.twig', [
302318
'base_template' => \sprintf('@JoliMediaEasyAdmin/%s.html.twig', $template),
303319
'breadcrumb' => $this->generateBreadcrumb($currentKey, $routeName),
@@ -307,7 +323,8 @@ public function list(AdminContext $adminContext, Request $request, string $key =
307323
'current_key' => $currentKey,
308324
'delete_directory_form' => $this->createDeleteDirectoryForm($key)->createView(),
309325
'directories' => $directories,
310-
'medias' => $this->getOriginalStorage()->listMedias($currentKey, recursive: false),
326+
'medias' => $paginator->getResults(),
327+
'paginator' => $paginator,
311328
'parent_key' => \dirname($currentKey),
312329
'rename_directory_form' => $this->createRenameDirectoryForm($key)->createView(),
313330
'route_name' => $routeName,

0 commit comments

Comments
 (0)