From e76d77dabc79c78581cd7537d1572c423aab0dbe Mon Sep 17 00:00:00 2001 From: Matthias-Kelvin Daous Date: Tue, 7 Oct 2025 12:58:14 +0200 Subject: [PATCH 1/4] add "decorating your data" part --- development/components/grid/_index.md | 82 ++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/development/components/grid/_index.md b/development/components/grid/_index.md index aaa2b55fc9..8d74fdb695 100644 --- a/development/components/grid/_index.md +++ b/development/components/grid/_index.md @@ -352,8 +352,88 @@ prestashop.core.grid.product_grid_factory: - '@prestashop.core.grid.filter.form_factory' # core service needed by grid factory - '@prestashop.core.hook.dispatcher' # core service needed by grid factory ``` +And we are done! -And we are done! Let's see how to use it and render it in the page. +#### Decorating your data + +Sometimes, you may need to decorate or modify your data in order to format it in a specific way. In this case, you can create your own decorator service. + +First, declare the decorator service in your YAML configuration: + +```yaml +my.namespace.grid.data_provider.product_grid_data_factory_decorator: + class: 'My\Namespace\Grid\Data\ProductGridDataFactoryDecorator' + arguments: + - '@prestashop.core.grid.data.factory.product_data_factory' # our data factory +``` + +Then you have to create the corresponding class. You can alter the records in the `applyModifications` function. + +```php +productGridDataFactory = $productGridDataFactory; + } + + public function getData(SearchCriteriaInterface $searchCriteria) + { + $productData = $this->productGridDataFactory->getData($searchCriteria); + + $productRecords = $this->applyModifications($productData->getRecords()); + + return new GridData( + $productRecords, + $productData->getRecordsTotal(), + $productData->getQuery() + ); + } + + private function applyModifications(RecordCollectionInterface $products) + { + $modifiedProducts = []; + + foreach ($products as $product) { + # Basic example on how to alter the record + $product['name'] = ucfirst($product['name']); + + $modifiedProducts[] = $product; + } + + return new RecordCollection($modifiedProducts); + } +} + +``` + +Finally, we override the default `Grid Factory` service to use our decorated data factory instead of the original one. + +```yaml +# Configure Grid factory to use services we have implemented +prestashop.core.grid.product_grid_factory: + class: 'PrestaShop\PrestaShop\Core\Grid\GridFactory' + arguments: + - '@prestashop.core.grid.definition.factory.product_grid_definition_factory' + - '@my.namespace.grid.data_provider.product_grid_data_factory_decorator' # our decorator service + - '@prestashop.core.grid.filter.form_factory' + - '@prestashop.core.hook.dispatcher' +``` + +Now let's see how to use the grid and render it in the page. ### Rendering Grid From 4dd69c78b5a9c5f555f465d9eedee5fb09144a30 Mon Sep 17 00:00:00 2001 From: MattKelvin Date: Fri, 10 Oct 2025 10:56:48 +0200 Subject: [PATCH 2/4] Apply suggestion from @kpodemski explain utility and improve style Co-authored-by: Krystian Podemski --- development/components/grid/_index.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/development/components/grid/_index.md b/development/components/grid/_index.md index 8d74fdb695..dae0b05e1b 100644 --- a/development/components/grid/_index.md +++ b/development/components/grid/_index.md @@ -356,7 +356,9 @@ And we are done! #### Decorating your data -Sometimes, you may need to decorate or modify your data in order to format it in a specific way. In this case, you can create your own decorator service. +Sometimes you may need to modify or decorate your data to format it in a specific way. In this case, you can create a custom decorator service. + +This is useful when you want to keep your data retrieval logic untouched and apply formatting, transformations, or additional computed fields before rendering the grid. First, declare the decorator service in your YAML configuration: From 1713ae9703c0636add383a089d65376645540612 Mon Sep 17 00:00:00 2001 From: MattKelvin Date: Fri, 10 Oct 2025 10:57:08 +0200 Subject: [PATCH 3/4] Apply suggestion from @kpodemski better style, spelling Co-authored-by: Krystian Podemski --- development/components/grid/_index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/development/components/grid/_index.md b/development/components/grid/_index.md index dae0b05e1b..f0e5efeb40 100644 --- a/development/components/grid/_index.md +++ b/development/components/grid/_index.md @@ -369,7 +369,7 @@ my.namespace.grid.data_provider.product_grid_data_factory_decorator: - '@prestashop.core.grid.data.factory.product_data_factory' # our data factory ``` -Then you have to create the corresponding class. You can alter the records in the `applyModifications` function. +Then, create the corresponding class. You can alter the records inside the `applyModifications()` method. ```php Date: Fri, 10 Oct 2025 10:57:25 +0200 Subject: [PATCH 4/4] Apply suggestion from @kpodemski better style, spelling Co-authored-by: Krystian Podemski --- development/components/grid/_index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/development/components/grid/_index.md b/development/components/grid/_index.md index f0e5efeb40..e8aaa510ed 100644 --- a/development/components/grid/_index.md +++ b/development/components/grid/_index.md @@ -422,7 +422,7 @@ final class ProductGridDataFactoryDecorator implements GridDataFactoryInterface ``` -Finally, we override the default `Grid Factory` service to use our decorated data factory instead of the original one. +Finally, override the default `GridFactory` service to use the decorated data factory instead of the original one. ```yaml # Configure Grid factory to use services we have implemented