diff --git a/development/components/grid/_index.md b/development/components/grid/_index.md index aaa2b55fc9..e8aaa510ed 100644 --- a/development/components/grid/_index.md +++ b/development/components/grid/_index.md @@ -352,8 +352,90 @@ 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 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: + +```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, create the corresponding class. You can alter the records inside the `applyModifications()` method. + +```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, 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 +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