Skip to content

lukepass/SonataAnnotationBundle

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sonata Annotation Bundle

Adds Annotations for Sonata Admin.

The point is to reduce noise, having lots of admin classes with just mappings that don't do anything else should be avoided. Add annotations to your models and you are done. If you need something that is not covered by this bundle, create admin class instead.

This bundle was greatly inspired by IbrowsSonataAdminAnnotationBundle

PHP Version Latest Stable Version Latest Unstable Version

Build Status Coverage Status Scrutinizer Code Quality

Documentation

Installation

1. Add dependency with composer

composer require kunicmarko/sonata-annotation-bundle

2. Register the bundle in your Kernel

return [
    //...
    KunicMarko\SonataAnnotationBundle\SonataAnnotationBundle::class => ['all' => true],
];

Configuration

By default we scan all files in your src directory, if you save your entities somewhere else you can change the directory:

sonata_annotation:
    directory: '%kernel.project_dir%/src/'

How to use

Instead of creating class for Admin it is enough to just add annotation to your entity.

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;

/**
 * @Sonata\Admin("Category")
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
    /**
     * @Sonata\FormField()
     * @Sonata\ListField()
     * @Sonata\ShowField()
     * @Sonata\DatagridField()
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;
}

Clear cache:

bin/console cache:clear

And you will see Admin appear in your sidebar.

Annotations

Admin

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;
use App\Controller\YourCRUDController;
use App\Admin\YourAdmin;

/**
 * @Sonata\Admin(
 *     label="Category",
 *     managerType="orm",
 *     group="Category",
 *     showInDashboard=true,
 *     keepOpen=true,
 *     onTop=true,
 *     icon="<i class='fa fa-user'></i>",
 *     labelTranslatorStrategy="sonata.admin.label.strategy.native",
 *     labelCatalogue="App",
 *     pagerType="simple",
 *     controller=YourCRUDController::class,
 *     serviceId="app.admin.category",
 *     admin=YourAdmin::class,
 *     code="admin_code",
 * )
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
}

Access

If you are using role handler as described here you can add permission per role with this annotation.

This annotation can be used without Admin annotation present. If you have an admin class for your entity you can still use this annotation.

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;

/**
 * @Sonata\Admin("Category")
 *
 * @Sonata\Access("ROLE_CLIENT", permissions={"LIST", "VIEW", "EXPORT"})
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
}

AddChild

You can read more about this here.

This annotation can be used without Admin annotation present. If you have an admin class for your entity you can still use this annotation.

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;

/**
 * @Sonata\Admin("Category")
 * @Sonata\AddChild(class=Post::class, field="category")
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
}
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;

/**
 * @Sonata\Admin("Post")
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Post
{
    /**
     * @ORM\ManyToOne(targetEntity="Category")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    private $category;
}

FormField

You can specify action option that would allow you to have different fields or have different configuration for the same field for create and edit action. If not set, field will be used for create and edit. Besides that, you are able to set the position of the field. Position 1 would be the first field to render and higher numbers after. If field doesn't have position, it will be rendered after all fields with position.

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;

/**
 * @Sonata\Admin("Category")
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
    /**
     * @Sonata\FormField(
     *      action="create",
     *      type="",
     *      options={},
     *      fieldDescriptionOptions={},
     *      position=1 
     * )
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @Sonata\FormField(
     *      type="",
     *      options={},
     *      fieldDescriptionOptions={},
     *      position=2
     * )
     *
     * @ORM\Column(name="description", type="string", length=255)
     */
    private $description;
    
    /**
     * @Sonata\FormField(
     *      action="create",
     *      type=TextType::class
     * )
     *
     * @Sonata\FormField(
     *      action="edit",
     *      type=TextareaType::class
     * )
     *
     * @ORM\Column(name="something", type="string", length=255)
     */
    private $something;
}

ShowField

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;

/**
 * @Sonata\Admin("Category")
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
    /**
     * @Sonata\ShowField(
     *      type="",
     *      fieldDescriptionOptions={}
     * )
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;
    
    /**
     * @Sonata\ShowField()
     */
    public function showThis(): string
    {
        return 'show value';
    }
}

ShowAssociationField

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;

/**
 * @Sonata\Admin("Category")
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
    /**
     * @Sonata\ShowAssociationField(
     *      field="name",
     *      type="",
     *      fieldDescriptionOptions={}
     * )
     *
     * @Sonata\ShowAssociationField(
     *      field="email",
     *      type="",
     *      fieldDescriptionOptions={}
     * )
     *
     * @ORM\ManyToOne(targetEntity="Owner")
     * @ORM\JoinColumn(name="owner_id", referencedColumnName="id")
     */
    private $owner;
}

ListField

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;

/**
 * @Sonata\Admin("Category")
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
    /**
     * @Sonata\ListField(identifier=true) 
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @Sonata\ListField(
     *      type="",
     *      fieldDescriptionOptions={},
     *      identifier=false
     * )
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @Sonata\ListField()
     */
    public function listThis(): string
    {
        return 'list value';
    }
}

ListAssociationField

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;

/**
 * @Sonata\Admin("Category")
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
    /**
     * @Sonata\ListAssociationField(
     *      field="name",
     *      type="",
     *      fieldDescriptionOptions={},
     *      identifier=false
     * )
     * 
     * @Sonata\ListAssociationField(
     *      field="email",
     *      type="",
     *      fieldDescriptionOptions={},
     *      identifier=false
     * )
     *
     * @ORM\ManyToOne(targetEntity="Owner")
     * @ORM\JoinColumn(name="owner_id", referencedColumnName="id")
     */
    private $owner;
}

DatagridField

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;

/**
 * @Sonata\Admin("Category")
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
    /**
     * @Sonata\DatagridField(
     *      type="",
     *      fieldDescriptionOptions={},
     *      filterOptions={},
     *      fieldType="",
     *      fieldOptions={}
     * )
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;
}

DatagridAssociationField

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;

/**
 * @Sonata\Admin("Category")
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
    /**
     * @Sonata\DatagridAssociationField(
     *      field="name",
     *      type="",
     *      fieldDescriptionOptions={},
     *      filterOptions={},
     *      fieldType="",
     *      fieldOptions={}
     * )
     *
     * @Sonata\DatagridAssociationField(
     *      field="email",
     *      type="",
     *      fieldDescriptionOptions={},
     *      filterOptions={},
     *      fieldType="",
     *      fieldOptions={}
     * )
     *
     * @ORM\ManyToOne(targetEntity="Owner")
     * @ORM\JoinColumn(name="owner_id", referencedColumnName="id")
     */
    private $owner;
}

ExportField

You can add annotation to fields/method that you want to export, also you can add label for the field, if left blank field name will be used as label.

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;

/**
 * @Sonata\Admin("Category")
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
    /**
     * @Sonata\ExportField()
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @Sonata\ExportField("Custom Name")
     *
     * @ORM\Column(name="tag", type="string", length=255)
     */
    private $tag;

    /**
     * @Sonata\ExportField()
     */
    public function exportThis(): string
    {
        return 'export value';
    }
}

ExportAssociationField

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;

/**
 * @Sonata\Admin("Category")
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
    /**
     * @Sonata\ExportAssociationField(
     *      field="name",
     *      label="Owner"
     * )
     *
     * @Sonata\ExportAssociationField(
     *      field="email",
     *      label="Email"
     * )
     *
     * @ORM\ManyToOne(targetEntity="Owner")
     * @ORM\JoinColumn(name="owner_id", referencedColumnName="id")
     */
    private $owner;
}

ExportFormats

You can customize the export formats you want to allow, if this annotation is not present, all formats are shown.

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;

/**
 * @Sonata\Admin("Category")
 *
 * @Sonata\ExportFormats({"json", "xml"})
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
    /**
     * @Sonata\ExportField()
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;
}

AddRoute

Add custom routes to your admin class:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;
use App\Controller\YourCRUDController;

/**
 * @Sonata\Admin(
 *     label="Category",
 *     controller=YourCRUDController::class
 * )
 *
 * @Sonata\AddRoute("import")
 * @Sonata\AddRoute(name="send_mail", path="{id}/send_mail")
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
}

RemoveRoute

remove already existing routes:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;
use App\Controller\YourCRUDController;

/**
 * @Sonata\Admin(
 *     label="Category",
 *     controller=YourCRUDController::class
 * )
 *
 * @Sonata\RemoveRoute("edit")
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
}

ActionButton

This will add button next to your add button in a list view. Here you can find how the template should look like.

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;
use App\Controller\YourCRUDController;

/**
 * @Sonata\Admin(
 *     label="Category",
 *     controller=YourCRUDController::class
 * )
 *
 * @Sonata\AddRoute(name="import", path="/import")
 *
 * @Sonata\ActionButton("import_action_button.html.twig")
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
}

DashboardAction

This will add button to your dashboard block for this entity. Here you can find how the template should look like.

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;
use App\Controller\YourCRUDController;

/**
 * @Sonata\Admin(
 *     label="Category",
 *     controller=YourCRUDController::class
 * )
 *
 * @Sonata\AddRoute(name="import", path="/import")
 *
 * @Sonata\DashboardAction("import_dashboard_button.html.twig")
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
}

ListAction

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;
use App\Controller\YourCRUDController;

/**
 * @Sonata\Admin(
 *     label="Category",
 *     controller=YourCRUDController::class
 * )
 *
 * @Sonata\AddRoute(name="import", path="/import")
 *
 * @Sonata\ListAction("show")
 * @Sonata\ListAction("edit")
 * @Sonata\ListAction("delete")
 * @Sonata\ListAction(name="import", options={"template"="import_list_button.html.twig"})
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
}

DatagridValues

As explained here.

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;
use App\Controller\YourCRUDController;

/**
 * @Sonata\Admin("Category")
 *
 * @Sonata\DatagridValues({"_sort_by":"p.name"})
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
    /**
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;
}

Extending The Admin

Sometimes you need to do something custom and this bundle can't help you with that but you still want to use annotations for most of the other stuff. You can extend our admin class KunicMarko\SonataAnnotationBundle\Admin\AnnotationAdmin and overwrite the methods you want.

<?php

namespace App\Admin;

use KunicMarko\SonataAnnotationBundle\Admin\AnnotationAdmin;

class YourAdmin extends AnnotationAdmin
{
    //do what you want
}

And then in your entity you just provide that class

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;
use App\Admin\YourAdmin;

/**
 * @Sonata\Admin(
 *     label="Category",
 *     admin=YourAdmin::class
 * )
 *
 * @ORM\Table
 * @ORM\Entity
 */
class Category
{
}

About

Annotations for Sonata Admin

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 96.0%
  • Gherkin 1.6%
  • Shell 1.3%
  • Makefile 1.1%