Skip to content

Instantly share code, notes, and snippets.

@NicolasBadey
Last active December 31, 2015 13:09
Show Gist options
  • Save NicolasBadey/7991350 to your computer and use it in GitHub Desktop.
Save NicolasBadey/7991350 to your computer and use it in GitHub Desktop.
Sonata pour l'IIM

##Dans composer.json

    "sonata-project/doctrine-orm-admin-bundle": "dev-master"

puis faire php composer.phar update

##Dans app/appKernel.php ajouter les bundle :

    new Sonata\CoreBundle\SonataCoreBundle(),
    new Sonata\BlockBundle\SonataBlockBundle(),
    new Sonata\jQueryBundle\SonatajQueryBundle(),
    new Knp\Bundle\MenuBundle\KnpMenuBundle(),
    new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
    new Sonata\AdminBundle\SonataAdminBundle(),

##Dans config.yml:

sonata_doctrine_orm_admin:
  # default value is null, so doctrine uses the value defined in the configuration
  entity_manager: ~

  templates:
      form:
          - SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig
      filter:
          - SonataDoctrineORMAdminBundle:Form:filter_admin_fields.html.twig
      types:
          list:
              array:      SonataAdminBundle:CRUD:list_array.html.twig
              boolean:    SonataAdminBundle:CRUD:list_boolean.html.twig
              date:       SonataAdminBundle:CRUD:list_date.html.twig
              time:       SonataAdminBundle:CRUD:list_time.html.twig
              datetime:   SonataAdminBundle:CRUD:list_datetime.html.twig
              text:       SonataAdminBundle:CRUD:base_list_field.html.twig
              trans:      SonataAdminBundle:CRUD:list_trans.html.twig
              string:     SonataAdminBundle:CRUD:base_list_field.html.twig
              smallint:   SonataAdminBundle:CRUD:base_list_field.html.twig
              bigint:     SonataAdminBundle:CRUD:base_list_field.html.twig
              integer:    SonataAdminBundle:CRUD:base_list_field.html.twig
              decimal:    SonataAdminBundle:CRUD:base_list_field.html.twig
              identifier: SonataAdminBundle:CRUD:base_list_field.html.twig

          show:
              array:      SonataAdminBundle:CRUD:show_array.html.twig
              boolean:    SonataAdminBundle:CRUD:show_boolean.html.twig
              date:       SonataAdminBundle:CRUD:show_date.html.twig
              time:       SonataAdminBundle:CRUD:show_time.html.twig
              datetime:   SonataAdminBundle:CRUD:show_datetime.html.twig
              text:       SonataAdminBundle:CRUD:base_show_field.html.twig
              trans:      SonataAdminBundle:CRUD:show_trans.html.twig
              string:     SonataAdminBundle:CRUD:base_show_field.html.twig
              smallint:   SonataAdminBundle:CRUD:base_show_field.html.twig
              bigint:     SonataAdminBundle:CRUD:base_show_field.html.twig
              integer:    SonataAdminBundle:CRUD:base_show_field.html.twig
              decimal:    SonataAdminBundle:CRUD:base_show_field.html.twig

sonata_block:
   default_contexts: [cms]
   blocks:
       sonata.admin.block.admin_list:
           contexts:   [admin]
       sonata.block.service.text:
       sonata.block.service.action:
       sonata.block.service.rss:

##Dans app/config/routing.yml

    admin:
        resource: '@SonataAdminBundle/Resources/config/routing/sonata_admin.xml'
        prefix: /admin/sonata
        
    _sonata_admin:
        resource: .
        type: sonata_admin
        prefix: /admin/sonata

Création d’une classe Admin

La classe Admin représente le mappage de votre entité et de ses sections d’administration (forms, lists, show, create, edit…). La façon la plus simple de créer une classe Admin, est d’hériter de la classe: Sonata\AdminBundle\Admin\Admin

La classe ArticleAdmin dans IIM/BlogBundle/Admin/ArticleAdmin:

<?php
namespace IIM\BlogBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;

class ArticleAdmin extends Admin
{
        //liste des champs modifiables dans l'edit
        protected function configureFormFields(FormMapper $formMapper)
        {
            $formMapper
                ->with('General')
                    ->add('title')
                    ->add('content')
                    ->add('enabled')
                ->end()
            ;
        }
    
        //liste des champs qui seront visibles dans la liste des enregistrements
        protected function configureListFields(ListMapper $listMapper)
        {
            $listMapper
                ->addIdentifier('title')
                ->add('content')
                ->add('_action', 'actions', array(
                    'actions' => array(
                    'view' => array(),
                    'edit' => array(),
                    'delete' => array(),
                    )
                ))
            ;
        }
    
        //liste des champs qui pourraient servir à trier les enregistrements dans la liste des enregistrements 
        protected function configureDatagridFilters(DatagridMapper $datagridMapper)
        {
              $datagridMapper
                  ->add('title')
                  ->add('author', null, array('field_options' => array('expanded' => true, 'multiple' => true)))
                  ->add('content')
                  ->add('enabled')
              ;
        }
        
        // champs visibles dans show
        protected function configureShowField(ShowMapper $show)
        {
            $show
                ->add('title')
                ->add('content')
                ->add('enabled')
            ;
        }
    }

Création d’un service Admin

Pour informer SonataAdminBundle de votre nouvelle classe Admin, vous avez besoin de créer un service et de le relier au framework en définissant le tag sonata.admin

il faut un service par class admin

Exemple avec /IIM/BlogBundle/Resources/config/admin.yml:

services:
    iim.blog.admin.article:
      class: IIM\BlogBundle\Admin\ArticleAdmin
      tags:
        - { name: sonata.admin, manager_type: orm, group: Blog, label: Articles}
      arguments: [null, IIM\BlogBundle\Entity\Article,SonataAdminBundle:CRUD]

group = regroupe les class possédant le même "group" pour les retrouver plus facilement

label = nom de la class admin dans la vue

pour finir il faut lier le service avec la config générale, app/config/config.yml

imports:
    - { resource: @IIMBlogBundle/Resources/config/admin.yml } 

##dans app/config/security.yml

temporairement changer

    - { path: ^/admin/, role: ROLE_ADMIN }

en

    - { path: ^/admin/, role: ROLE_USER }

##faites à la console

    php app/console assets:install

##Allez, logué avec un User sur

    app_dev.php/admin/sonata/dashboard

Admirez l'admin en Twitter Bootstrap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment