Trinity Grid
Trinity grid is part of Trinity package., (*1)
Description:, (*2)
- Converts an array of entities to array of strings.
- Array of string is convert to JSON:
Documentation
1) Create Grid
Create class extended by BaseGrid. Add templates for change attributes value., (*3)
class ProductGrid extends BaseGrid
{
public function setUp()
{
$this->addTemplate("ProductGrid.html.twig");
}
}
Set up
addTemplate - add new template for changing values.
setColumnFormat - for basic chage value of date or simple text edit.
Template:, (*4)
cell_attributeName for change value., (*5)
{% block cell_name %} Template edit - {{ value }} {% endblock %}
Available variables are:
- value
- row - entity object, (*6)
2) Register grid
File: services.yml, (*7)
services:
trinity.grid.test.product:
class: Trinity\Bundle\GridBundle\Tests\Functional\Grid\ProductGrid
tags:
- { name: trinity.grid, alias: product }
Tag name: trinity.grid
Alias is mandatory value for searching grids from entity name., (*8)
Product entity has product alias., (*9)
Grid filters
Filters are used to change the attribute values., (*10)
Special grid:
Special filters are used for changing specific attribute (id, name, etc.)., (*11)
1) Create filter:, (*12)
Filter -> BaseFilter or FilterInterface!, (*13)
Attribute 'name' is necessary!, (*14)
class IdFilter extends BaseFilter
{
/**
* @var string
*/
protected $name = 'id';
/**
* @param string|object|int|bool $input
* @param array $arguments
* @return string
*/
function process($input, array $arguments = []) : string
{
return $input.'.';
}
}
2) Grid registration:, (*15)
File: service.yml
Tag: trinity.grid.filter, (*16)
trinity.grid.filter.id:
class: Trinity\Bundle\GridBundle\Filter\IdFilter
tags:
- {name: "trinity.grid.filter"}, (*17)
3) Set up filter for current grid:, (*18)
$this->setColumnFilter('id', 'id');
First attribute 'id' -> column name., (*19)
Second attribute -> filter name., (*20)
Global filters
For global filter must be set attribute 'global' to TRUE;, (*21)
Attribute 'name' is not necessary., (*22)
class ObjectFilter extends BaseFilter
{
protected $global = true;
/**
* @param string|object|int|bool $input
* @param array $arguments
* @return string
*/
function process($input, array $arguments = []) : string
{
if ((is_object($input) && method_exists($input, 'getName'))) {
$input = $input->getName();
} elseif (is_object($input)) {
$input = (string)$input;
}
return $input;
}
}
3) Parse entity array to array of strings
From container pull service 'trinity.grid.manager', (*23)
$manager = $container->get('trinity.grid.manager');
Parse array of entities:, (*24)
$stringArray = $manager->convertEntitiesToArray(
$this->getArrayOfEntities(), // array of entities
['id', 'name', 'description', 'nonexistentColumn', 'createdAt'] // columns
);
From the entity name is found grid. $stringArray is must be convert to JSON., (*25)