Skip to content
Advertisement

Sonata Admin: how validate remove action?

How check conditions before remove action and set flash message in sonata-admin?

I do not want remove super-user. My current code:

public function preRemove($object)
    {
        parent::preRemove($object);
        if ($object->getId() === User::SUPER_USER_ID) {
            throw new AccessDeniedException();
        }
    }

Its throw exception. I need send flash message in admin-panel.

Advertisement

Answer

Thank you Ryuk Lee, He made me research the code 🙂

Solution:

public function preRemove($object)
    {
        parent::preRemove($object);
        if ($object->getId() === User::SUPER_USER_ID) {
            $this->getRequest()->getSession()->getFlashBag()->add('sonata_flash_error','Not delete super user');
            throw new ModelManagerException();
        }
    }

ModelManagerException – This is an exception that will make the sonata work properly. Do not delete the object and write a error message in the admin panel, without “success” message. Work only debug = false.

$kernel = new AppKernel('dev', false);

But I met a problem, toogle error messages (“more”):

enter image description here

2 solutions:

1) override template

config.xml

sonata_admin:
    templates:
        layout: 'admin/layout.html.twig'

layout.html.twig

{% extends '@SonataAdmin/standard_layout.html.twig' %}

{% block notice %}
    {% include 'admin/flash_messages.html.twig' %}
{% endblock notice %}

flash_messages.html.twig

{% for type in sonata_flashmessages_types() %}
    {% set domain = domain is defined ? domain : null %}
    {% set messages = sonata_flashmessages_get(type, domain) %}
    {% if messages|length > 0 %}
        {% for message in messages %}
            <div class="alert alert-{{ type|sonata_status_class }} alert-dismissable">
                <button
                        type="button"
                        class="close"
                        data-dismiss="alert"
                        aria-hidden="true"
                        aria-label="{{ 'message_close'|trans({}, 'SonataCoreBundle') }}">
                    &times;
                </button>
                {{ message | raw }}
            </div>
        {% endfor %}
    {% endif %}
{% endfor %}

Result:

enter image description here

2) Override Admin controller.

serivices.yml

admin.user.admin:
    class: AppBundleAdminAdminUserAdmin
    arguments: [~, AppBundleEntityUser, AppBundleControllerAdminAdminUserCRUDController]
    tags:
      - { name: sonata.admin, manager_type: orm, label: 'Admins' }

AdminUserCRUDController

class AdminUserCRUDController extends CRUDController
{
   public function deleteAction($id)
   {
       $redirectResponse = parent::deleteAction($id);
       /** @var FlashBagInterface $flashBag */
       $flashBag = $this->container->get('session')->getFlashBag();
       if($errors = $flashBag->get('sonata_flash_error')){
           $flashBag->set(
               'sonata_flash_error',
               implode('. ',array_unique($errors))
           );
       }
       return $redirectResponse;
   }

Result:

enter image description here

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement