EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

ArrayField - formatValue

Open seb-jean opened this issue 4 years ago • 3 comments

Hi,

formatValue() with ArrayField does not work. The value remains the same. It is not formatted.

My code:

public function configureFields(string $pageName): iterable
{
    yield ArrayField::new('sports')
        ->formatValue(fn ($items) => array_map(fn($item) => $this->translator->trans('sports'.'.'.$item), $items))
    ;
}

seb-jean avatar Jul 05 '21 07:07 seb-jean

Having the same issue here. Instead of displaying the formatted value, it displays the original value.

ArrayField::new('spokenLanguages')
    ->formatValue(function ($languages) {
        return \array_map(
            function ($language) {
                return LanguageEnum::toLabel($language);
            },
            $languages
        );
    })
    ->hideOnIndex()
,

WouterCypers avatar Jul 13 '21 13:07 WouterCypers

same issue with me

saddit avatar Nov 26 '21 08:11 saddit

Same problem, except formatValue() works on index page but not on detail page:

$expand = Action::DETAIL === $pageName;
yield ArrayField::new('sousSecteurs')
            ->formatValue(function (mixed $value) use ($expand) {
                if ($value instanceof Collection && $value->count() > 0) {
                    $noms = $value->map(fn ($secteur) => $secteur->intitule)->toArray();
                    if ($expand) {
                        return implode(', ', $noms);
                    }
                    $display = array_slice($noms, 0, 2);
                    $suffix = count($noms) > 2 ? '...' : '';

                    return implode(', ', $display).$suffix;
                } else {
                    return $value;
                }
            })
            ->hideOnForm()
        ;

Logic is executed as intended when $expand is false (on index page) but is ignored on detail page

mano-lis avatar Mar 07 '24 14:03 mano-lis