Easyadminbundle: Custom newAction in Controller

Created on 2 Mar 2018  路  4Comments  路  Source: EasyCorp/EasyAdminBundle

Sorry About my questions , I'm beginner in easyadmin and Symfony
I need to define a custom newAction method in my controller
I want to handle everything in easy admin , like a default newAction
but

      //1- need form to get data 
        $data = $form->getData();

        if ($form->isSubmitted() && $form->isValid()) {


            $handler = $this->getEvaluateHandler();

            $evaluate = $handler->createEvaluate($data->getTarget(), $segments);

            $handler->persistEvaluate($evaluate);

            return $this->redirectToReferrer();
        }
        //2- need back to default easyadmin template new for this entity 
        return $this->render();

    } catch (\Exception $exception) {

        return new Response($exception->getMessage());
    }
}

1- need form to get data
2- need back to default easyadmin template new for this entity

Most helpful comment

The solution provided by @uncle-scrooge is the correct one:

1) If you just want to pass some custom parameters to your variables, use the recently added renderTemplate() method to pass them. Look at this test for inspiration: https://github.com/EasyCorp/EasyAdminBundle/blob/master/tests/Fixtures/AppTestBundle/Controller/CustomTemplateParametersController.php
2) If you want to override the newAction() features completely to do vey custom things, then override the method entirely.
3) If you want to override just some things instead of everything, you can override some of the methods used by the newAction(), such as createNewEntity(), createNewForm(), persistEntity(), etc.
4) Lastly, you can also listen to any of the events triggered during the execution of newAction() to customize the application using events.

The 2), 3) and 4) options are explained in https://symfony.com/doc/master/bundles/EasyAdminBundle/book/complex-dynamic-backends.html.

Closing as fixed then.

All 4 comments

Does any body have any idea about this problem??

Just need form and ability to back easyadmin default form (defined in yml file)

Hello @Farshadi73

Is the logic of your newAction method very different from the one provided by easyAdmin ?

If not you may consider using events to perform your operations.

For exemple you may listen to the pre-persist event of your entity to get it's data.

Please take a look at the link below
https://symfony.com/doc/current/bundles/EasyAdminBundle/book/complex-dynamic-backends.html#customization-based-on-symfony-events

English is not my native langage so i may misundertood your problem so do not hesitate to correct me.

@Farshadi73

Looking at https://github.com/EasyCorp/EasyAdminBundle/blob/1.x/src/Controller/AdminController.php

There is a method called "renderTemplate" thay may be helpful to you.

I think that looking at the "newAction" method may be helpful to.

The solution provided by @uncle-scrooge is the correct one:

1) If you just want to pass some custom parameters to your variables, use the recently added renderTemplate() method to pass them. Look at this test for inspiration: https://github.com/EasyCorp/EasyAdminBundle/blob/master/tests/Fixtures/AppTestBundle/Controller/CustomTemplateParametersController.php
2) If you want to override the newAction() features completely to do vey custom things, then override the method entirely.
3) If you want to override just some things instead of everything, you can override some of the methods used by the newAction(), such as createNewEntity(), createNewForm(), persistEntity(), etc.
4) Lastly, you can also listen to any of the events triggered during the execution of newAction() to customize the application using events.

The 2), 3) and 4) options are explained in https://symfony.com/doc/master/bundles/EasyAdminBundle/book/complex-dynamic-backends.html.

Closing as fixed then.

Was this page helpful?
0 / 5 - 0 ratings