madebyjo_l
madebyjo_l

Reputation: 28

Array to string conversion in EasyAdmin 3 (Symfony 5)

I'm trying to display a json array on the EasyAdmin detail page. I read here Is there a way to represent a JSON field in EasyAdmin 3? that you can use ArrayField in EasyAdmin 3 to display a json array, which would make sense to me as that is the only field in EasyAdmin that could display it so I added it like this:

public function configureFields(string $pageName): iterable
{
    return [
        ArrayField::new('status', $this->translator->trans('form.label.status'))->onlyOnDetail(),
    ];
}

But it gives me this error: An exception has been thrown during the rendering of a template ("Notice: Array to string conversion"). Do I need to add something else to it or does it not work at all?

Upvotes: 1

Views: 2369

Answers (3)

LvanderRee
LvanderRee

Reputation: 436

What I do to render my json fields is

use EasyCorp\Bundle\EasyAdminBundle\Field\CodeEditorField;
use App\Controller\Admin\Form\Type\Field\JsonCodeEditorType;

// ...

class MyCrudController extends AbstractCrudController
{

//...
    public function configureFields(string $pageName): iterable
    {
        return [

            CodeEditorField::new('status')
                ->setLanguage('javascript')
                ->setFormType(JsonCodeEditorType::class)
                ->formatValue(function ($value, $entity) {
                    return json_encode($value, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT);
                })
                ->setFormTypeOption('attr', ['style' => 'min-height: 30px'])
        ]
    }
}

with

<?php

namespace Arpha\DigistekkerBundle\Controller\Admin\Form\Type\Field;

use EasyCorp\Bundle\EasyAdminBundle\Form\Type\CodeEditorType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\FormBuilderInterface;


class JsonCodeEditorType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder
            ->addModelTransformer(new CallbackTransformer(
                static fn($object) => json_encode($object, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT),
                static fn($json) => json_decode($json, true, 512, \JSON_THROW_ON_ERROR)
            ));
    }

    public function getParent()
    {
        return CodeEditorType::class;
    }
}

Upvotes: 0

Ahmed Ghiloubi
Ahmed Ghiloubi

Reputation: 401

I found a workaround that resolved the issue in my situation where I wanted just to show JSON on details page

So in your entity add a get function as an unmapped field as indicated in the official documentaiton https://symfony.com/bundles/EasyAdminBundle/current/fields.html#unmapped-fields

for example

public function getJsonField() {
  return json_encode($this->yourEntityJsonAttribute);
}

then in configureFields function in your CrudController add your field like this

TextAreaField::new('jsonField')->onlyOnDetail()

You can also read the json attribute and generate an HTML string in the getJsonField function then in configure field just add renderAsHtml in th field like this

TextAreaField::new('jsonField')->onlyOnDetail()->renderAsHtml()

Wish this suits your case too, Good luck

Upvotes: 2

Henry Robben
Henry Robben

Reputation: 1

change "status" to a multiplicity "statuses" Worked for me with changing "print" to "prints"

Upvotes: 0

Related Questions