Maxim
Maxim

Reputation: 3946

JSON array to table Sonata admin bundle

I am trying to load a json array into sonata, this should happen when i have th edit form (Only in the edit / view form)

The reason is that i have to be able to view applications, this is the code i have so far

class ApplicationUserAdmin extends Admin
{

    public $supportsPreviewMode = false;
    protected $security;

    public function getNewInstance()
    {
        $instance = parent::getNewInstance();
        return $instance;
    }

    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {

        $transformer = new JsonArrayToTableTransformer();

        $formMapper
            ->add('application', 'entity', array('class' => 'MaximModuleApplicationBundle:Application'))
            ->add('denied', 'checkbox', array('label' => 'Denied', 'required' => false))
            ->add($formMapper->create('details', 'json')
                ->addViewTransformer($transformer))
        ;
    }

    // Fields to be shown on filter forms
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('application')
            ->add('denied')
        ;
    }

    // Fields to be shown on lists
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('id')
            ->add('user')
            ->add('application')
            ->add('date')
            ->add('denied')
        ;
    }

    protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
    {
        if (!$childAdmin && !in_array($action, array('edit'))) {
            return;
        }

        $admin = $this->isChild() ? $this->getParent() : $this;
        $id = $admin->getRequest()->get('id');

        $menu->addChild(
            'view',
            array('uri' => $admin->generateUrl('edit', array('id' => $id)))
        );

        $menu->addChild(
            'replies',
            array('uri' => $admin->generateUrl('sonata.admin.module.application.replies.list', array('id' => $id)))
        );
    }

    public function setSecurityContext(SecurityContext $security)
    {
        $this->security = $security;
    }
} 

A small concept of what i am trying to get: Image

Upvotes: 2

Views: 3222

Answers (1)

Maxim
Maxim

Reputation: 3946

I solved this by adding a template value and then a for loop on the array

{% extends 'SonataAdminBundle:CRUD:base_show_field.html.twig' %}


{% block field %}
    {% for key, v in value %}
        <div>
            <h5>{{ key }}</h5>
            <p>{{ v|purify }}</p>
        </div>
    {% endfor %}
{% endblock %}


 protected function configureShowFields(ShowMapper $showMapper)
 {
    $showMapper
        ->add('application')
        ->add('denied')
        ->add('details', 'string', array('template' => 'MyBundle:Admin:jsonToTable.html.twig'))
    ;
 }

Upvotes: 2

Related Questions