Skip to content

Commit

Permalink
Label: Add TranslatableInterface support
Browse files Browse the repository at this point in the history
  • Loading branch information
hlecorche committed Aug 14, 2023
1 parent 3d0d5aa commit 6803920
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"symfony/security-csrf": "^5.4|^6.0",
"symfony/service-contracts": "^1.1.6|^2|^3",
"symfony/translation": "^5.4|^6.0",
"symfony/translation-contracts": "^2.3|^3.0",
"symfony/twig-bridge": "^5.4|^6.0",
"symfony/twig-bundle": "^5.4|^6.0",
"symfony/validator": "^5.4|^6.0",
Expand Down
7 changes: 4 additions & 3 deletions src/Crud/CrudColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Validation;
use Symfony\Contracts\Translation\TranslatableInterface;

final class CrudColumn
{
Expand Down Expand Up @@ -53,7 +54,7 @@ public function __construct(array $options)
'alias',
]);
$resolver->setDefaults([
'label' => fn (Options $options): string => $options['id'],
'label' => fn (Options $options): string|TranslatableInterface => $options['id'],
'sortable' => true,
'displayed_by_default' => true,
'alias_sort' => fn (Options $options): string => $options['alias'],
Expand All @@ -64,7 +65,7 @@ public function __construct(array $options)
new Length(max: 100, maxMessage: 'The column id "{{ value }}" is too long. It should have {{ limit }} character or less')
));
$resolver->setAllowedTypes('alias', 'string');
$resolver->setAllowedTypes('label', 'string');
$resolver->setAllowedTypes('label', ['string', TranslatableInterface::class]);
$resolver->setAllowedTypes('sortable', 'bool');
$resolver->setAllowedTypes('displayed_by_default', 'bool');
$resolver->setAllowedTypes('alias_sort', ['string', 'array']);
Expand All @@ -83,7 +84,7 @@ public function getAlias(): string
return $this->options['alias'];
}

public function getLabel(): string
public function getLabel(): string|TranslatableInterface
{
return $this->options['label'];
}
Expand Down
11 changes: 10 additions & 1 deletion src/Form/Type/DisplaySettingsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Count;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Contracts\Translation\TranslatableInterface;

class DisplaySettingsType extends AbstractType
{
Expand All @@ -34,8 +35,16 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'constraints' => new NotBlank(),
]);

$columnsChoices = $options['columns_choices'];
$builder->add('displayedColumns', ChoiceType::class, [
'choices' => array_flip($options['columns_choices']),
'choices' => array_keys($columnsChoices),
'choice_label' => function (string $choice, string $key, mixed $value) use ($columnsChoices): string|TranslatableInterface {
if (\array_key_exists($choice, $columnsChoices)) {
return $columnsChoices[$choice];
}

return $choice;
},
'multiple' => true,
'expanded' => true,
'label' => 'display_settings.displayed_columns',
Expand Down
3 changes: 2 additions & 1 deletion src/Twig/CrudExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatableInterface;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
Expand Down Expand Up @@ -330,7 +331,7 @@ public function th(Environment $environment, string $columnId, Crud $crud, array

return $value;
});
$resolver->setAllowedTypes('label', ['null', 'string']);
$resolver->setAllowedTypes('label', ['null', 'string', TranslatableInterface::class]);
$options = $resolver->resolve($this->buildOptions('crud_th', $options, $crud));

// If the column is not to be shown, returns empty
Expand Down
3 changes: 2 additions & 1 deletion tests/Functional/App/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Ecommit\CrudBundle\Crud\Crud;
use Ecommit\CrudBundle\Tests\Functional\App\Entity\TestUser;
use Ecommit\CrudBundle\Tests\Functional\App\Form\Searcher\UserSearcher;
use Symfony\Component\Translation\TranslatableMessage;

class UserController extends AbstractCrudController
{
Expand All @@ -31,7 +32,7 @@ protected function getCrudOptions(): array
$crudConfig = $this->createCrudConfig(static::getCrudName())
->addColumn('username', 'u.username', 'username', ['displayed_by_default' => false])
->addColumn('firstName', 'u.firstName', 'first_name')
->addColumn('lastName', 'u.lastName', 'last_name')
->addColumn('lastName', 'u.lastName', new TranslatableMessage('last_name'))
->setQueryBuilder($queryBuilder)
->setMaxPerPage([5, 10, 50], 5)
->setDefaultSort('firstName', Crud::ASC)
Expand Down

0 comments on commit 6803920

Please sign in to comment.