Developed with love by KnpLabs Hire us for your project!
63

EasyAdminExtensionBundle

by alterphp

Provides some additional features to https://github.com/easycorp/EasyAdminBundle for Symfony

EasyAdmin Extension

Build Status SensioLabsInsight Coverage Status

EasyAdmin Extension provides some useful extensions to EasyAdmin admin generator for Symfony.

:exclamation: This bundle requires at least PHP 7.0 and Symfony 3.0 components or stack.

Installation

Step 1: Download the Bundle

$ composer require alterphp/easyadmin-extension-bundle

This command requires you to have Composer installed globally, as explained
in the Composer documentation.

Step 2: Enable the Bundle

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new AlterPHP\EasyAdminExtensionBundle\EasyAdminExtensionBundle(),
        );
    }

    // ...
}

Step 3: Replace EasyAdmin controller

Instead of loading routes from EasyAdminBundle AdminController, load them from EasyAdminExtensionBundle AdminController.

Symfony 4 directory structure :
```yaml

config/routes/easy_admin.yaml

easy_admin:
resource: '@EasyAdminExtensionBundle/Controller/AdminController.php'
prefix: /admin
type: annotation

...


Former Symfony 2/3 directory structure :
```yaml
# app/config/routing.yml
easy_admin:
    resource: "@EasyAdminExtensionBundle/Controller/AdminController.php"
    prefix:   /admin
    type:     annotation

# ...

If you have defined your own admin controllers, make them extend EasyAdminExtensionBundle admin controller.

Features

Filter list and search on request parameters

  • EasyAdmin allows filtering list with dql_filter configuration entry. But this is not dynamic and must be configured as an apart list in easy_admin configuration.*

This extension allows to dynamically filter lists by adding filters parameter in the URL parameters. Having a list of books at URL <url-to-admin>?action=list&entity=Book with a releaseYear field, you can filter on books releasd in 2016 by requesting <url-to-admin>?action=list&entity=Book&filters[entity.releaseDate]=2016. It only matches exact values, but you can chain them. To request books released in 2015 and 2016, you must request <url-to-admin>?action=list&entity=Book&filters[entity.releaseDate][]=2015&filters[entity.releaseDate][]=2016.

This filters parameter is transmitted to the referer used for post update/delete/create redirection AND for search !

Register your own form types with a short name (aliasing form types)

You have custom form types that you want to use in the EasyAdmin configuration. You can already register them with FQCN ... but it's quite boring and makes the admin massively enlarged. This feature allows you to define your own form types with short names, by configuration.

Let's see how to register them with those 2 examples (enum and statusable) :

easy_admin_extension:
    custom_form_types:
        enum: Admin\Form\Type\EnumType
        statusable: Admin\Form\Type\StatusableType

Embed lists as form widgets

Embed your EasyAdmin lists in edit views. This is really useful for *ToMany relations.

Use pre-configured type embedded_list in the form definition :

easy_admin:
    entities:
        Event:
            class: Tm\EventBundle\Entity\Event
        Promoter:
            class: AppBundle\Entity\Promoter
            form:
                fields:
                    # ...
                    - { type: group, label: Events, css_class: 'col-sm-12', icon: calendar }
                    - { property: events, label: '', type: embedded_list, type_options: { entity: Event, filters: { 'entity.promoter': 'form:parent.data.id' } } }

But in many cases, the EmbeddedListHelper guesses type_options for you, and you just have to write :

easy_admin:
    entities:
        Event:
            class: Tm\EventBundle\Entity\Event
        Promoter:
            class: AppBundle\Entity\Promoter
            form:
                fields:
                    # ...
                    - { type: group, label: Events, css_class: 'col-sm-12', icon: calendar }
                    - { property: events, label: '', type: embedded_list }

Let's see the result !

Embedded list example

Define access permissions

Global minimum role access

You can define a minimum role to access the EasyAdmin controller (any action handled by the controller) :

easy_admin_extension:
    minimum_role: ROLE_ADMIN

This is just a global restriction, that should live with a security firewall as described in Symfony documentation.

Per entity action role permissions

You can also define role permissions per entity action :

easy_admin:
    entities:
        Product:
            class: App\Entity\Product
            list:
                role: ROLE_ADMIN_PRODUCT_LIST
            search:
                role: ROLE_ADMIN_PRODUCT_SEARCH
            new:
                role: ROLE_ADMIN_PRODUCT_NEW
            edit:
                role: ROLE_ADMIN_PRODUCT_EDIT
            show:
                role: ROLE_ADMIN_PRODUCT_SHOW
            delete:
                role: ROLE_ADMIN_PRODUCT_DELETE

Above configuration define a required role per action for Product entity. This is too verbose, isn't it ? Let's sum up as following :

easy_admin:
    entities:
        Product:
            class: App\Entity\Product
            role_prefix: ROLE_ADMIN_PRODUCT

Entity role_prefix defines all actions required roles by appending the action name to the prefix.

Use template show vertical boostrap

Design EasyAdmin configuration:

easy_admin:
    design:
        templates:
            show: '@EasyAdminExtension/default/show_vertical.html.twig'

Run tests

Run following command :

$ ./vendor/phpunit/phpunit/phpunit

OR using Docker and Docker Compose :

$ docker-compose run --rm phpunit

Run code quality tools

PHP CS Fixer

Locally with Docker :

docker-compose run --rm php /app/vendor/bin/php-cs-fixer fix --config=/app/.php_cs /app/src

PHPStan

Locally with Docker :

docker-compose run --rm php /app/vendor/bin/phpstan analyse -c /app/phpstan.neon --level=6 /app/src

License

This software is published under the MIT License

  • Fix menu re-indexing
    By alterphp, 24 days ago
  • Fix code style
    By alterphp, 24 days ago
  • Reindex menu items after pruning
    By alterphp, 24 days ago
  • Write a test that fails when pruning breaks menu items indexation
    By alterphp, 24 days ago
  • Fix embedded list guesser
    By alterphp, 28 days ago
  • Fix guessing embedded list default filters when no entity FQCN found
    By alterphp, 28 days ago
  • fix typo
    By alterphp, 1 month ago
  • fix show_vertical.html.twig
    By alterphp, 1 month ago
  • Add template show vertical boostrap
    By alterphp, 1 month ago
  • Fix embeddedList in NEW views
    By alterphp, 2 months ago
  • Fixed spaces
    By alterphp, 2 months ago
  • Added missing role "NEW"
    By alterphp, 2 months ago
  • autocomplete action access permission is mapped on list access permission
    By alterphp, 3 months ago
  • fix exception message
    By alterphp, 3 months ago
  • adapt fixtures to embedded_lists
    By alterphp, 3 months ago
  • PHPStan level 6 compliance
    By alterphp, 3 months ago
  • PHP-CS-Fixer OK
    By alterphp, 3 months ago
  • EmbeddedList guesser
    By alterphp, 3 months ago
  • generic docker usage in README
    By alterphp, 3 months ago
  • PHP-CS-Fixer config + PHPStan level 6 compliance
    By alterphp, 3 months ago
  • Allow sorting embedded list by instance
    By alterphp, 3 months ago
  • Tests + renaming variables
    By alterphp, 3 months ago
  • Add embedded_list open_in_new_tab_link config option
    By alterphp, 3 months ago
  • Fix tests
    By alterphp, 3 months ago
  • Update to new easyadmin-bundle route
    By alterphp, 4 months ago
  • Create CODE_OF_CONDUCT.md
    By web-flow, 4 months ago
  • Init toggles in embeddedList at load
    By alterphp, 4 months ago
  • adds a block for "Open in a new tab" link in embedded lists
    By alterphp, 4 months ago
  • Fix documentation
    By web-flow, 4 months ago
  • Repair menu pruned based on entity actions in links
    By alterphp, 5 months ago