kunicmarko/sonata-annotation-bundle
A Symfony bundle that lets you configure Sonata Admin classes with PHP annotations instead of YAML or PHP config. Define admin services plus list, form, show, and datagrid fields directly in annotations, keeping your admin setup compact and close to your entities.
composer require kunicmarko/sonata-annotation-bundle
The package is archived and the codebase has not had PHP 8.x / Sonata Admin 4–5 compatibility work, so expect to pin a version and possibly relax transitive constraints on a modern stack. Note that releases are still trickling out (1.2.1 adds a datagrid position option), so pin an exact version rather than a range you have not tested.bundles.php or AppKernel), after SonataAdminBundle.src/Entity or src/Model paths) in the bundle config. If the config key is not obvious, copy the block from the package's Resources/config or its test fixture.use KunicMarko\SonataAnnotationBundle\Annotation as Sonata;
/**
* @Sonata\Admin("Product", group="Catalog")
*/
class Product
{
/**
* @Sonata\DatagridField(position=1)
* @Sonata\FormField
*/
private $name;
/**
* @Sonata\DatagridField(position=2, sortable=true)
* @Sonata\FormField(type="integer")
*/
private $price;
}
bin/console cache:clear) so the compiler pass re-scans, then open /admin/dashboard. If the entry appears, the wiring works.Where to look first: the README for the annotation catalogue, then src/Annotation/ for the exact attributes each annotation accepts (DatagridField, DatagridAssociationField, FormField, ShowField, Admin), and tests/ for a working annotated model plus its expected admin output. Those two directories are more reliable than the docs for edge cases, and they are the only place you will see options like position documented.
First use case: an internal back-office CRUD screen for a small entity whose list/form/show are 90% scalar fields. That is exactly what the annotations cover cheaply; anything more elaborate will push you back to a hand-written AbstractAdmin.
@Admin declares the admin: first argument is the label, plus options such as group for dashboard grouping, manager type, controller, and templates. One annotation per model class.@DatagridField, @FormField, @ShowField, and @DatagridAssociationField for to-one/to-many columns) are placed on properties or accessors. Combine them on the same property so datagrid, form, and show stay in sync; annotate accessors when the value is computed rather than stored.@DatagridField and @DatagridAssociationField accept a position option. Use it to pin the datagrid order instead of relying on property declaration order, keep the numbers spaced (10, 20, 30) so you can insert a column later without renumbering, and remember it only affects the datagrid — form and show order are still driven by annotation order.@FormField(type="choice", options={"required"=false}), @DatagridField(label="Unit price", sortable=true, position=30). Anything the annotation does not expose must be set in a real admin class.group on @Admin is the main day-to-day lever for keeping a large dashboard navigable.@DatagridAssociationField for association columns, and the bundle's child/parent annotations for one-to-many nesting if your version supports it; otherwise declare the child admin separately and attach it with addChild in a normal admin class.AbstractAdmin subclass instead of fighting the annotations. Mixing both in one codebase is normal and supported by Sonata itself.bin/console debug:container --tag=sonata.admin, note its id, and either alias/replace it or extend its class to override configureDatagridFields() / configureFormFields().position addition is a small feature patch, not evidence of active maintenance. On a modern stack, verify composer resolution before committing; on legacy Sonata 3.x/PHP 7.x it is fine.position option on the datagrid annotations), so it is safe to bump within 1.x. Confirm you are not already setting column order via a custom configureDatagridFields() override — if you are, the annotation and the override will race and the override wins, making position look broken.position does not appear, that is almost always the cause.@DatagridField simply yields a missing column rather than an error — easy to miss in review. The same applies to position: an unpositioned field is not an error, it just lands wherever the traversal put it.sonata_type_model_autocomplete, and custom types generally need a hand-written admin; the annotation option array is not a full substitute for configureFormFields().use ... as Sonata;) so @Admin does not collide with other packages' annotations or Doctrine's own.debug:container --tag=sonata.admin to find the generated service, then dump the admin's getDatagridBuilder/getFormBuilder/getListBuilder in a throwaway test to see what the annotations actually produced — including the resolved column order. Faster than guessing.How can I help you explore Laravel packages today?