Search code examples
symfonysonata-admin

How to remove routes depending the object Sonata Admin


I would like to remove the delete route for certain objects (which are linked to others...).

Is there a way to remove Routes in the configureRoutes method depending on the edited object (for example the id)?

Or is there a way to do it in the configureFormFields method ?


Solution

  • EDIT: This is a very old answer, so it may or may not work in your case

    To get the current object inside XXXAdmin class use:

    $this->getSubject();

    protected function configureFormFields(FormMapper $formMapper) {
      $product = $this->getSubject();
    
      if ($product->getId()) { // editing
        //
      }
      //...
    }
    
    public function configureRoutes(\Sonata\AdminBundle\Route\RouteCollection $collection)  {
      $product = $this->getSubject();
    
      if ($product->getId()) { // editing
        $collection->remove('route');
      }
    
    }