Pavel
Pavel

Reputation: 155

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.

Upvotes: 3

Views: 1928

Answers (3)

7ochem
7ochem

Reputation: 2340

I have used the CRUD controller preDelete() hook for this.

class MyCrudController extends CRUDController
{
    protected function preDelete(Request $request, $object): ?RedirectResponse
    {
        if (/* some blocking condition */) {
            $this->addFlash(
                'sonata_flash_error',
                $this->trans(
                    'flash_not_allowed_because_of_reasons',
                    ['%name%' => $this->escapeHtml($object)],
                    'SonataAdminBundle'
                )
            );
            return $this->redirectTo($object);
        }

        return null;
    }
}

Within the Sonata Admin CRUD Controller it will run this check:

    $preResponse = $this->preDelete($request, $object);
    if (null !== $preResponse) {
        return $preResponse;
    }

So if you return a Response from your preDelete() method, then Sonata Admin won't continue the delete. And with this method you will have only one flash message.

Upvotes: 0

Pavel
Pavel

Reputation: 155

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: AppBundle\Admin\AdminUserAdmin
    arguments: [~, AppBundle\Entity\User, AppBundle\Controller\Admin\AdminUserCRUDController]
    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

Upvotes: 1

Ryuk Lee
Ryuk Lee

Reputation: 744

This is the override function. If you want to stop remove or remove if everything ok you just put parent::preRemove($object); or parent::remove($object); at the end of the function.

public function preRemove($object)
{
    if ($object->getId() === User::SUPER_USER_ID) {
        $this->getRequest()->getSession()->getFlashBag()->add(
            'error',
            'Title, Abstract and Small tile Media are required'
        );
        return;
    }
    //other code to check here

    parent::preRemove($object); 
}

public function remove($object)
{
    if ($object->getId() === User::SUPER_USER_ID) {
        $this->getRequest()->getSession()->getFlashBag()->add(
            'error',
            'Title, Abstract and Small tile Media are required'
        );
        return;
    }
    //other code to check here

    parent::remove($object); 
}

Upvotes: 1

Related Questions