Creating your first grid
In order to use grids, you need to register your entity as a Sylius resource. Let us assume you have a Supplier model in your application, which represents a supplier of goods in your shop and has several fields, including name, description and enabled.
In order to make it a Sylius resource, you need to add the AsResource attribute and implement ResourceInterface.
namespace App\Entity;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Model\ResourceInterface;
#[AsResource]
class Supplier implements ResourceInterface
{
// ...
}That's it! Your class is now a resource. In order to learn what it means, please refer to the SyliusResourceBundle documentation.
Grid Maker
You can create your grid using the Symfony Maker bundle.
$ bin/console make:gridGrid Definition
Now we can configure our first grid:
Using your grid on an index operation
The SyliusResourceBundle allows you to use a grid into an index operation:
This will generate the following path:
Now, your new grid should look like this when accessing the index on /admin/suppliers/:

Defining Filters
To allow users to search for specific items in the grid, you can use filters.
What will it look like in the admin panel?

Advanced filtering : relationships
What about filtering by fields of related entities? For instance if you would like to filter your suppliers by their country of origin, which is a property of the associated address entity.
There are 2 ways you can do this :
you can resort to a custom repository method and pass it to the Grid Builder via
setRepositoryMethod()but this will only work when using Doctrineor you can join on your entities directly inside your provider when using a custom data provider
Custom repository method (Doctrine-only)
Custom data provider
Here is a simple example of a custom data provider. You're obviously free to actually fetch your data in whatever way suits your need, by calling a repository of any kind (Doctrine, API, in-memory,..) directly or call a query bus for instance.
Then, this example Doctrine repository uses a JOIN statement on our related Address Entity.
Adding your entity filter to your grid
Then you can simply insert your filter inside the grid.
Default Sorting
You can define by which field you want the grid to be sorted and how using orderBy() .
Then in the fields section, indicate that the field can be used for sorting with setSortable():
If your field is not of a "simple" type, e.g. a Twig template with a specific path, you can enable sorting with the following definition:
Pagination
You can limit how many items are visible on each page by providing an array of integers into the limits parameter. The first element of the array will be treated as the default.
Actions Configuration
Next step is adding some actions to the grid: create, update and delete.
First, we need to create these operations on our resource:
These new operations are now available:
Then we need to add these operations into our Grid using Actions.
This activates such a view on the /admin/suppliers/ path:

Your grid is ready to use!
Last updated