Create Admin tab for an entity with SonataAdminBundle

Publié le par Sébastien Flamant

Today, we will see how to add a new admin tab to manage an entity with Sonata. For example you want to manage Animals, you could have this entity :

<?php

namespace My\AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

use Doctrine\Common\Collections\ArrayCollection;

/**

* My\AppBundle\Entity\Animal

*

* @ORM\Table()

*/

class Animal

{

/**

* @var integer $id

*

* @ORM\Column(name="id", type="integer")

* @ORM\Id

* @ORM\GeneratedValue(strategy="AUTO")

*/

private $id;

/**

* @var string $name

*

* @ORM\Column(name="name", type="string", length=64)

*/

private $name;

/* Getters and setters */

} 

Now, you have to create an Admin folder in your bundle directory and create AnimalAdmin.php

The AnimalAdmin extends of Sonata Admin main class.

 <?php

namespace My\AppBundle\Admin;

use Sonata\AdminBundle\Form\FormMapper;

use Sonata\AdminBundle\Datagrid\DatagridMapper;

use Sonata\AdminBundle\Datagrid\ListMapper;

use Sonata\AdminBundle\Show\ShowMapper;

use Sonata\AdminBundle\Admin\Admin ;

class AnimalAdmin extends Admin

{

public function getTemplate($name)

{

switch ($name) {

/*case 'edit':

return 'MyAppBundle::my-custom-edit.html.twig';

break; */

default:

return parent::getTemplate($name);

break;

}

}

protected function configureListFields(ListMapper $listMapper)

{

$listMapper

->add('id')

->add('name')

;

}

/**

* {@inheritdoc}

*/

protected function configureShowFields(ShowMapper $showMapper)

{

$showMapper

->with('General')

->add('id')

->add('name')

->end()

;

}

/**

* {@inheritdoc}

*/

// Fields wich are shown in you Animal Admin Tab

protected function configureFormFields(FormMapper $formMapper)

{

$formMapper

->with('General')

->add('id')

->add('name')

->end()

;

}

}

When you select the edit button, you can redirect to your own template, here « my-custom-edit.html.twig », you just have to uncomment the case ‘edit’. Otherwise, the default template from sonataAdmin will be used.

Finally, you have to create the service. To do this, you just have to create services.xml in Ressources/config of your bundle directory.


<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="<a href="http://symfony.com/schema/dic/services">http://symfony.com/schema/dic/services</a> http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>

<parameter key="sonata.animal.admin.animal.class">Vet\BlogBundle\Admin\AnimalAdmin</parameter>

<parameter key="sonata.animal.admin.animal.entity">Vet\BlogBundle\Entity\Animal</parameter>

<parameter key="sonata.animal.admin.animal.controller">SonataAdminBundle:CRUD</parameter>

<parameter key="sonata.animal.admin.animal.translation_domain">VetBlogBundle</parameter>

<parameter key="sonata.animal.admin.groupname">animal</parameter>

</parameters>

<services>

<service id="sonata.animal.admin.animal" class="%sonata.animal.admin.animal.class%">

<tag name="sonata.admin" manager_type="orm" group="%sonata.animal.admin.groupname%" label="animals" label_catalogue="VetBlogBundle" label_translator_strategy="sonata.admin.label.strategy.underscore" />

<argument />

<argument>%sonata.animal.admin.animal.entity%</argument>

<argument>%sonata.animal.admin.animal.controller%</argument>

<call method="setTranslationDomain">

<argument>%sonata.animal.admin.animal.translation_domain%</argument>

</call>

</service>

</services>

</container>

You can now manage your Animals entity on your Sonata backend.

Cette entrée a été publiée dans Symfony 2 Utils. Vous pouvez la mettre en favoris avec ce permalien.



Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

*


9 * = twenty seven

Vous pouvez utiliser ces balises et attributs HTML : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>