From c85d034bbd3c652dbbc8befd39c174c423ed8a11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9C=C3=A9?= Date: Fri, 14 Jun 2019 15:42:23 +0200 Subject: [PATCH] Adds max_results config for embedded_lists (#143) * Adds max_results config for embedded_lists --- README.md | 1 + src/Configuration/ShowViewConfigPass.php | 3 +++ src/Controller/EasyAdminController.php | 4 +++- src/Form/Type/EasyAdminEmbeddedListType.php | 3 +++ src/Resources/views/default/field_embedded_list.html.twig | 1 + src/Resources/views/form/bootstrap_4.html.twig | 1 + src/Resources/views/includes/_include_embedded_list.html.twig | 3 +++ tests/Configuration/ShowViewConfigPassTest.php | 2 ++ 8 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3e67c84..71dbbca 100644 --- a/README.md +++ b/README.md @@ -235,6 +235,7 @@ Available options are : - `entity`: Entity config name (key under the EasyAdmin `entities` config) - `filters`: Request filters to apply on the list - `hidden_fields`: List of fields (columns) to hide from list fields config +- `max_results`: Number of items par page (list.max_results config is used if not defined) - `sort`: Sort to apply - `parent_entity_fqcn`: Parent entity FQCN in order to guess default filters (only when embedded in *SHOW* view, almost never required) - `parent_entity_property`: Matching property name on parent entity FQCN (only when embedded in *SHOW* view, if `property` is not an ORM field) diff --git a/src/Configuration/ShowViewConfigPass.php b/src/Configuration/ShowViewConfigPass.php index 6938dc4..a71eb40 100644 --- a/src/Configuration/ShowViewConfigPass.php +++ b/src/Configuration/ShowViewConfigPass.php @@ -101,6 +101,9 @@ private function processTemplateOptions(string $type, array $fieldMetadata) if (!isset($templateOptions['hidden_fields'])) { $templateOptions['hidden_fields'] = []; } + if (!isset($templateOptions['max_results'])) { + $templateOptions['max_results'] = null; + } if (isset($templateOptions['sort'])) { $sortOptions = $templateOptions['sort']; $templateOptions['sort'] = [ diff --git a/src/Controller/EasyAdminController.php b/src/Controller/EasyAdminController.php index b8ece88..a7e5f08 100644 --- a/src/Controller/EasyAdminController.php +++ b/src/Controller/EasyAdminController.php @@ -20,7 +20,9 @@ protected function embeddedListAction() { $this->dispatch(EasyAdminEvents::PRE_LIST); - $paginator = $this->findAll($this->entity['class'], $this->request->query->get('page', 1), $this->config['list']['max_results'], $this->request->query->get('sortField'), $this->request->query->get('sortDirection'), $this->entity['list']['dql_filter']); + $maxResults = (int) $this->request->query->get('max-results', $this->config['list']['max_results']); + + $paginator = $this->findAll($this->entity['class'], $this->request->query->get('page', 1), $maxResults, $this->request->query->get('sortField'), $this->request->query->get('sortDirection'), $this->entity['list']['dql_filter']); $this->dispatch(EasyAdminEvents::POST_LIST, ['paginator' => $paginator]); diff --git a/src/Form/Type/EasyAdminEmbeddedListType.php b/src/Form/Type/EasyAdminEmbeddedListType.php index 0ee1dc6..42cf438 100644 --- a/src/Form/Type/EasyAdminEmbeddedListType.php +++ b/src/Form/Type/EasyAdminEmbeddedListType.php @@ -66,6 +66,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) $view->vars['filters'] = $filters; $view->vars['hidden_fields'] = $options['hidden_fields']; + $view->vars['max_results'] = $options['max_results']; if ($options['sort']) { $sort['field'] = $options['sort'][0]; @@ -83,10 +84,12 @@ public function configureOptions(OptionsResolver $resolver) ->setDefault('entity', null) ->setDefault('filters', []) ->setDefault('hidden_fields', []) + ->setDefault('max_results', null) ->setDefault('sort', null) ->setAllowedTypes('entity', ['null', 'string']) ->setAllowedTypes('filters', ['array']) ->setAllowedTypes('hidden_fields', ['array']) + ->setAllowedTypes('max_results', ['null', 'int']) ->setAllowedTypes('sort', ['null', 'string', 'array']) ; } diff --git a/src/Resources/views/default/field_embedded_list.html.twig b/src/Resources/views/default/field_embedded_list.html.twig index 17bda54..354260d 100644 --- a/src/Resources/views/default/field_embedded_list.html.twig +++ b/src/Resources/views/default/field_embedded_list.html.twig @@ -7,5 +7,6 @@ entity: field_options.template_options.entity, filters: filters, hidden_fields: field_options.template_options.hidden_fields, + max_results: field_options.template_options.max_results, sort: field_options.template_options.sort } only %} diff --git a/src/Resources/views/form/bootstrap_4.html.twig b/src/Resources/views/form/bootstrap_4.html.twig index 8c5422d..7f3715a 100644 --- a/src/Resources/views/form/bootstrap_4.html.twig +++ b/src/Resources/views/form/bootstrap_4.html.twig @@ -10,6 +10,7 @@ entity: entity, filters: filters, hidden_fields: hidden_fields, + max_results: max_results, sort: sort|default(null) } only %} {% endblock %} diff --git a/src/Resources/views/includes/_include_embedded_list.html.twig b/src/Resources/views/includes/_include_embedded_list.html.twig index 7ec2045..3563760 100644 --- a/src/Resources/views/includes/_include_embedded_list.html.twig +++ b/src/Resources/views/includes/_include_embedded_list.html.twig @@ -2,5 +2,8 @@ {% if sort is not null %} {% set action_params = action_params|merge({ sortField: sort.field, sortDirection: sort.direction}) %} {% endif %} +{% if 0 < max_results|default %} + {% set action_params = action_params|merge({ 'max-results': max_results}) %} +{% endif %} {{ render(path('easyadmin', action_params)) }} diff --git a/tests/Configuration/ShowViewConfigPassTest.php b/tests/Configuration/ShowViewConfigPassTest.php index d4d1503..c845a2c 100644 --- a/tests/Configuration/ShowViewConfigPassTest.php +++ b/tests/Configuration/ShowViewConfigPassTest.php @@ -62,6 +62,7 @@ public function testDefaultEmbeddedListShow() 'entity' => 'MyRelation', 'filters' => [], 'hidden_fields' => [], + 'max_results' => null, 'sort' => null, ], ], @@ -136,6 +137,7 @@ public function testDefinedEmbeddedListShow() 'filters' => ['bar' => 'baz'], 'sort' => ['field' => 'qux', 'direction' => 'ASC'], 'hidden_fields' => [], + 'max_results' => null, ], ], ],