Skip to content

Commit

Permalink
[FieldTypes] add vars option on DateTime & Twig field types
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Feb 18, 2025
1 parent 0f305fe commit 68a2e65
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/Bundle/Tests/Functional/GridUiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,21 @@ public function it_filters_books_by_author_when_an_author_is_used_in_join_in_que
$this->assertSame('A Study in Scarlet', $titles[0]);
}

/** @test */
public function it_renders_option_vars(): void
{
$this->client->request('GET', '/books/');

$data = $this->getCrawler()
->filter('th.text-end')
->each(
fn (Crawler $node): string => $node->text(),
)
;

$this->assertSame('Currency', $data[0] ?? null);
}

/** @test */
public function it_includes_all_rows_even_when_sorting_by_a_nullable_path(): void
{
Expand Down
1 change: 1 addition & 0 deletions src/Bundle/Tests/Provider/ServiceGridProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function test_grids_inheritance(): void
$this->assertEquals([
'title',
'author',
'currency',
'id',
], array_keys($gridDefinition->getFields()));

Expand Down
9 changes: 9 additions & 0 deletions src/Bundle/spec/Builder/Field/DateTimeFieldSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,13 @@ function it_creates_fields(): void
$field->shouldHaveType(FieldInterface::class);
$field->getName()->shouldReturn('createdAt');
}

function it_defines_var_options(): void
{
$field = $this::create('createdAt');
$field->setOption('vars', ['foo' => 'bar']);

$field->shouldHaveType(FieldInterface::class);
$field->getOptions()['vars']->shouldReturn(['foo' => 'bar']);
}
}
9 changes: 9 additions & 0 deletions src/Bundle/spec/Builder/Field/StringFieldSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,13 @@ function it_creates_fields(): void
$field->shouldHaveType(FieldInterface::class);
$field->getName()->shouldReturn('firstName');
}

function it_defines_var_options(): void
{
$field = $this::create('firstName');
$field->setOption('vars', ['foo' => 'bar']);

$field->shouldHaveType(FieldInterface::class);
$field->getOptions()->shouldReturn(['vars' => ['foo' => 'bar']]);
}
}
2 changes: 2 additions & 0 deletions src/Component/FieldTypes/DatetimeFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@ public function configureOptions(OptionsResolver $resolver): void
$resolver->setAllowedTypes('format', 'string');
$resolver->setDefault('timezone', $this->timezone);
$resolver->setAllowedTypes('timezone', ['null', 'string']);
$resolver->setDefined('vars');
$resolver->setAllowedTypes('vars', 'array');
}
}
2 changes: 2 additions & 0 deletions src/Component/FieldTypes/StringFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ public function render(Field $field, $data, array $options): string

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefined('vars');
$resolver->setAllowedTypes('vars', 'array');
}
}
2 changes: 2 additions & 0 deletions src/Component/spec/FieldTypes/DatetimeFieldTypeSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ function it_uses_timezone_parameter_as_default_timezone_option(
$resolver->setAllowedTypes('format', 'string')->willReturn($resolver)->shouldBeCalled();
$resolver->setDefault('timezone', 'Europe/Warsaw')->willReturn($resolver)->shouldBeCalled();
$resolver->setAllowedTypes('timezone', ['null', 'string'])->willReturn($resolver)->shouldBeCalled();
$resolver->setDefined('vars')->willReturn($resolver)->shouldBeCalled();
$resolver->setAllowedTypes('vars', 'array')->willReturn($resolver)->shouldBeCalled();

$this->configureOptions($resolver);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Application/config/sylius/grids.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ sylius_grid:
label: Nationality
path: author.nationality.name
sortable: author.nationality.name
currency:
type: string
label: Currency
path: price.currencyCode
sortable: price.currencyCode
options:
vars:
th_class: "text-end"
limits: [10, 5, 15]

app_author:
Expand Down
7 changes: 7 additions & 0 deletions tests/Application/config/sylius/grids/book.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
->setPath('author.nationality.name')
->setSortable(true, 'author.nationality.name'),
)
->addField(
StringField::create('currency')
->setLabel('Currency')
->setPath('price.currencyCode')
->setSortable(true, 'price.currencyCode')
->setOption('vars', ['th_class' => 'text-end']),
)
->setLimits([10, 5, 15]),
);
};
7 changes: 7 additions & 0 deletions tests/Application/src/Grid/BookGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
->setPath('author.nationality.name')
->setSortable(true, 'author.nationality.name'),
)
->addField(
StringField::create('currency')
->setLabel('Currency')
->setPath('price.currencyCode')
->setSortable(true, 'price.currencyCode')
->setOption('vars', ['th_class' => 'text-end']),
)
->addActionGroup(
ItemActionGroup::create(
ShowAction::create([
Expand Down
2 changes: 1 addition & 1 deletion tests/Application/templates/crud/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<tr>
{% for field in definition.fields %}
{% if field.enabled %}
<th class="sylius-table-column-{{ field.name }}">{{ field.label|trans }}</th>
<th class="sylius-table-column-{{ field.name }} {{ field.options.vars.th_class|default('') }}">{{ field.label|trans }}</th>
{% endif %}
{% if definition.actionGroups.item is defined and definition.getEnabledActions('item')|length > 0 %}
<th class="sylius-table-column-actions">Actions</th>
Expand Down

0 comments on commit 68a2e65

Please sign in to comment.