0

I˙m ṫrying to remove Add new button for /stageserver/list route.

I have tried with this code:

public function getBatchActions()
{
    $actions = parent::getBatchActions();

    if($this->hasRoute('/stageserver/list'))
    {
        $actions['remove'] = 'create';
    }

}

I have tried this next code, but it removes that route in all other admin sections.

public function configureRoutes(RouteCollection $collection)
{
   $collection->remove('create');
}

I can˙t find for specific route removal in Sonata docs, or perhaps I have missed it.

Bengall
  • 1,267
  • 2
  • 9
  • 13

1 Answers1

3

With Sonata, your Admin is related to a single entity.

Example, I have an entity named AppBundle\Entity\StageServer.

I create an Admin service to manage this entity :

admin.stage_server:
    class: AppBundle\Admin\StageServerAdmin
    public: true
    arguments: [~, AppBundle\Entity\StageServer, ~]
    tags:
        - { name: sonata.admin, manager_type: orm }

And a dedicated class for this service :

<?php

namespace AppBundle\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Route\RouteCollection;

class StageServerAdmin extends AbstractAdmin
{
    protected function configureRoutes(RouteCollection $collection)
    {
        $collection->remove('create');
    }
}

This should be enough to remove the ability to create AppBundle\Entity\StageServer objects.


edit

Solution using configureActionButtons method to remove create button only when onto the list action.

<?php

namespace AppBundle\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Route\RouteCollection;

class StageServerAdmin extends AbstractAdmin
{
    public function configureActionButtons($action, $object = null)
    {
        $buttons = parent::configureActionButtons($action, $object);
        if (in_array($action, array('list'))) {
            unset($buttons['create']);
        }

        return $buttons;
    }
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
Yann Eugoné
  • 1,311
  • 1
  • 8
  • 17
  • As I said, second code works, but it removes that route in other place where servers are meant to be added. Route `/stageserver/list` is meant just for checking, adding servers is in other section. That˙s why I tried with `if statement`. What I need is -> when on `/stageserver/list` route, remove **Add new** button, but when on other route where that element is needed, show it. – Bengall Jul 19 '17 at 16:00
  • you may try to use the `configureActionButtons` and remove the create button when you are on list : https://sonata-project.org/bundles/admin/master/doc/reference/advanced_configuration.html#actions-menu – Yann Eugoné Jul 19 '17 at 17:00
  • Yep, that works. I didn˙t write correct syntax that˙s why my code didnṫ worked. Btw, you wrote uncomplete code where is `unset` function, I have edited that part with missing code => `'create']);`Thanks for your help! – Bengall Jul 23 '17 at 17:51
  • Oups my bad, I did not wrote that code in an IDE... Thx Nigel Ren for the edit – Yann Eugoné Jul 24 '17 at 10:15