sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Enable adding child to tab menu with uri not related to admin class #2853

Closed webdevilopers closed 5 years ago

webdevilopers commented 9 years ago

Normally I add a child to the tab menu by generating the admin related Url like this:

    protected function configureTabMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
    {
        if (!$childAdmin && !in_array($action, array('edit', 'show', 'list'))) {
            return;
        }

        if ($this->isGranted('SHOW')) {
            $menu->addChild('Show Contract', array('uri' => $admin->generateUrl('show', array('id' => $id))));
        }
    }

But when I try to add an uri that is not related to an admin but for instance a standalone Controller:

class BundleController extends Controller
{
    /**
     * @Route("/qis/contract/{contractId}/bundle/egz", name="qis_contract_bundle_egz")
     */

like this:

$menu->addChild('EGZ Report', array('uri' => $this->generateUrl('qis_contract_bundle_egz', array('contractId' => $id, 'pdf' => 1))));

/*
$admin = $this->isChild() ? $this->getParent() : $this;
$menu->addChild('EGZ Report', array('uri' => $admin->generateUrl('qis_contract_bundle_egz', array('contractId' => $id, 'pdf' => 1))));
*/

I get an exception: _An exception has been thrown during the rendering of a template ("unable to find the route sonata.admin.contract.qis_contract_bundle_egz") in SonataAdminBundle:CRUD:base_edit.html.twig at line 34. _

The Url generator assumes there is an admin class and prefixes the uri with sonata.admin.contract..

How can I change this behaviour?

Possibly related issue:

webdevilopers commented 9 years ago

So far this is a possible workaround:

$container = $this->getConfigurationPool()->getContainer();
$menu->addChild('EGZ Report', array(
    'uri' => $container->get('router')->generate('qis_contract_bundle_egz', array('contractId' => $id, 'pdf' => 1))));

This could maybe done with an Injection too:

The problem with this workaround is that the generated URL is "incorrect" when looking a the show action of the parent where the nested Child(ren) are listed. Here is a different admin class example:

class CompanyAdmin extends Admin
{
    public $baseRouteName = 'qis_company';
    public $baseRoutePattern = '/company';

    protected function configureTabMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
    {
        if (!$childAdmin && !in_array($action, array('show'))) { return; }

        $admin = $this->isChild() ? $this->getParent() : $this;
        $id = $admin->getRequest()->get('id');

        // unable to find the route `sonata.admin.company.qis_company_branch_list`
        // @see https://github.com/sonata-project/SonataAdminBundle/issues/2853
//        $menu->addChild($this->trans('Branch List'), array('uri' => $admin->generateUrl('qis_company_branch_list', array('id' => $id))));

        $container = $this->getConfigurationPool()->getContainer();
        $menu->addChild($this->trans('Branch List'), array('uri' => $container->get('router')->generate('qis_company_branch_list', array('id' => $id))));
    }

    protected function configureShowFields(ShowMapper $showMapper)
    {
        $showMapper
            ->with('Company', array('class' => 'col-md-4'))
                ->add('phone')
                ->add('fax')
                ->add('street')
                ->add('city')
                ->add('country')
            ->end()
            ->with('Branches', array('class' => 'col-md-4'))
                ->add('branches', null, array('route' => array('name' => 'show')))
            ->end()
        ;
    }
}

Generated URL: http://localhost:8000/qis/acme/contract/branch/2/show?uniqid=s5523a4a581525&code=sonata.admin.branch&pcode=sonata.admin.company&puniqid=s5523a4a581575

Expected URL: http://localhost:8000/qis/company/1/branch/2/show (Generated correctly when coming from http://localhost:8000/qis/company/1/branch/list)

webdevilopers commented 9 years ago

Same problem when coming from a template:

<a href="{{ admin.generateUrl('admin_appbundle_contract_timekeepingentry_list', { 'id': object.id }) }}"></a>

results in: An exception has been thrown during the rendering of a template ("unable to find the route sonata.admin.contract.admin_appbundle_contract_timekeepingentry_list")

aivus commented 9 years ago

This issue was resolved after command rm -rf app/cache/*. app/console cache:clear doesn't help me.

Also, @webdevilopers you should call addChild in service. Like

<service id="acme.admin.test"
                 class="Acme\AcmeBundle\Admin\TestAdmin">
            <tag name="sonata.admin"
                 manager_type="orm"
                 group="test"
                 label="Test"/>
            <argument />
            <argument>Acme\AcmeBundle\Entity\Test</argument>
            <argument />
            <call method="addChild">
                <argument type="service" id="survey.admin.another"/>
            </call>
</service>
webdevilopers commented 9 years ago

I think we are talking about different things, @aivus .

The addChild method is called on the menu not the admin class since I stated: I try to add an uri that is not related to an admin.

It is a general problem with the added prefix to the url.

damienalexandre commented 9 years ago

:+1:

It look likes we even can't just link to another admin if they are not parent/child:

An exception has been thrown during the rendering of a template ("unable to find the route sonata.admin.gallery|sonata.admin.user.list") in SonataAdminBundle:CRUD:base_edit.html.twig at line 34.

$menu->addChild(
    "Users",
    $admin->generateMenuUrl('sonata.admin.user.list', array('filter' => ['gallery' => ['value' => $id]]))
);

Too bad this is not allowed :]

lucile-fievet commented 9 years ago

If the routes of Admin Class ( $baseRouteName and $baseRoutePattern ) are overriden, ( the one of the child or the one of the parent ) the chlid routes are not beeing generated http://stackoverflow.com/questions/28636229/child-admin-route-is-not-being-generated-sonata-admin-bundle

core23 commented 5 years ago

This issue is very old. Can you please check this against the latest stable / master version?

Feel free to reopen, if the error still exists.

A PR would be welcome to if you reopen this issue.

webdevilopers commented 5 years ago

Sorry, but I'm no longer working on this project.