Skip to content

Commit

Permalink
Fixed EasyCorp#6213 // Make ArrayField translatable
Browse files Browse the repository at this point in the history
  • Loading branch information
Geolim4 committed Mar 20, 2024
1 parent f63e526 commit 4ea1021
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Field/Configurator/ArrayConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

use function Symfony\Component\String\u;

/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
final class ArrayConfigurator implements FieldConfiguratorInterface
{
private TranslatorInterface $translator;

public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}

public function supports(FieldDto $field, EntityDto $entityDto): bool
{
return ArrayField::class === $field->getFieldFqcn();
Expand All @@ -40,10 +50,20 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
if (Crud::PAGE_INDEX === $context->getCrud()->getCurrentPage()) {
$values = $field->getValue();
if ($values instanceof PersistentCollection) {
$values = array_map(static fn ($item): string => (string) $item, $values->getValues());
$values = array_map(static fn($item): string => (string)$item, $values->getValues());
}

$field->setFormattedValue(u(', ')->join($values)->toString());
} else if (Crud::PAGE_DETAIL === $context->getCrud()->getCurrentPage() && is_iterable($field->getValue())) {
$field->setValue(
array_map(fn(mixed $value) => $value instanceof TranslatableInterface
? $value->trans($this->translator)
: $value,
iterator_to_array(
$field->getValue()
)
)
);
}
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@
->tag('form.type', ['alias' => 'ea_crud'])

->set(ArrayConfigurator::class)
->arg(0, service('translator.default'))

->set(AssociationConfigurator::class)
->arg(0, new Reference(EntityFactory::class))
Expand Down

0 comments on commit 4ea1021

Please sign in to comment.