Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@
* [Installation](grid/installation.md)
* [Creating your first grid](grid/your_first_grid.md)
* [Fields](grid/fields.md)
* [Creating a custom Action](grid/custom_action.md)
* [Creating a custom Bulk Action](grid/custom_bulk_action.md)
* [Actions](grid/actions.md)
* [Filters](grid/filters.md)
* [Creating a custom Filter](grid/custom_filter.md)
* [Advanced configuration](grid/advanced_configuration.md)
Expand Down
149 changes: 148 additions & 1 deletion docs/grid/custom_action.md → docs/grid/actions.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Creating a custom Action
# Actions

## Creating a custom Action

There are certain cases when built-in action types are not enough.

Expand Down Expand Up @@ -145,3 +147,148 @@ final class AdminSupplierGrid extends AbstractGrid implements ResourceAwareGridI
{% endcode %}
{% endtab %}
{% endtabs %}

## Creating a custom Bulk Action

In some cases, forcing the user to click a button for each item in a grid isn't practical.
Fortunately, you can take advantage of built-in bulk actions. However, these may not always be sufficient and might need customization.

To do this, simply create your own bulk action template and register it inside the `sylius_grid`.

In the template we will specify the button's icon to be `export` and its
colour to be `orange`.

{% code title="@App/Grid/BulkAction/export.html.twig" %}
```twig
{% import '@SyliusUi/Macro/buttons.html.twig' as buttons %}

{% set path = options.link.url|default(path(options.link.route)) %}

{{ buttons.default(path, action.label, null, 'export', 'orange') }}
```
{% endcode %}

Now configure the new action's template:

{% code title="config/packages/sylius_grid.yaml" lineNumbers="true" %}
```yaml
sylius_grid:
templates:
bulk_action:
export: "@App/Grid/BulkAction/export.html.twig"
```
{% endcode %}

From now on, you can use your new bulk action type in the grid configuration!

Let's assume that you already have a route for exporting by injecting
ids. Now, you can configure the grid action:

{% tabs %}
{% tab title="YAML" %}
{% code title="config/packages/sylius_grid.yaml" lineNumbers="true" %}
```yaml
sylius_grid:
grids:
app_admin_product:
...
actions:
bulk:
export:
type: export
label: Export Data
options:
link:
route: app_admin_product_export
parameters:
format: csv
```
{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code title="config/packages/sylius_grid.php" lineNumbers="true" %}
```php
<?php

use App\Entity\Product;
use Sylius\Bundle\GridBundle\Builder\Action\Action;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\BulkActionGroup;
use Sylius\Bundle\GridBundle\Builder\GridBuilder;
use Sylius\Bundle\GridBundle\Builder\Field\Field;
use Sylius\Bundle\GridBundle\Config\GridConfig;

return static function (GridConfig $grid) {
$grid->addGrid(GridBuilder::create('app_admin_product', Product::class)
->addActionGroup(
BulkActionGroup::create(
Action::create('export', 'export')
->setLabel('Export Data')
->setOptions([
'link' => [
'route' => 'app_admin_product_export',
'parameters' => [
'format' => 'csv',
],
]
])
)
)
)
};
```
{% endcode %}

OR

{% code title="src/Grid/AdminProductGrid.php" lineNumbers="true" %}
```php
<?php

declare(strict_types=1);

namespace App\Grid;

use App\Entity\Product;
use Sylius\Bundle\GridBundle\Builder\Action\Action;
use Sylius\Bundle\GridBundle\Builder\ActionGroup\BulkActionGroup;
use Sylius\Bundle\GridBundle\Builder\GridBuilderInterface;
use Sylius\Bundle\GridBundle\Grid\AbstractGrid;
use Sylius\Bundle\GridBundle\Grid\ResourceAwareGridInterface;

final class AdminProductGrid extends AbstractGrid implements ResourceAwareGridInterface
{
public static function getName(): string
{
return 'app_admin_product';
}

public function buildGrid(GridBuilderInterface $gridBuilder): void
{
$gridBuilder
->addActionGroup(
BulkActionGroup::create(
Action::create('export', 'export')
->setLabel('Export Data')
->setOptions([
'link' => [
'route' => 'app_admin_product_export',
'parameters' => [
'format' => 'csv',
],
]
])
)
)
;
}

public function getResourceClass(): string
{
return Product::class;
}
}
```
{% endcode %}
{% endtab %}
{% endtabs %}
144 changes: 0 additions & 144 deletions docs/grid/custom_bulk_action.md

This file was deleted.