Browse Source

[childAdmin] append the parent's id only if exist.

Abdellatif Ait boudad 11 years ago
parent
commit
b35581139a
2 changed files with 5 additions and 3 deletions
  1. 2 2
      Route/DefaultRouteGenerator.php
  2. 3 1
      Tests/Route/DefaultRouteGeneratorTest.php

+ 2 - 2
Route/DefaultRouteGenerator.php

@@ -57,7 +57,7 @@ class DefaultRouteGenerator implements RouteGeneratorInterface
     public function generateMenuUrl(AdminInterface $admin, $name, array $parameters = array(), $absolute = false)
     {
         // if the admin is a child we automatically append the parent's id
-        if ($admin->isChild()) {
+        if ($admin->isChild() && $admin->hasRequest() && $admin->getRequest()->attributes->has($admin->getParent()->getIdParameter())) {
             // twig template does not accept variable hash key ... so cannot use admin.idparameter ...
             // switch value
             if (isset($parameters['id'])) {
@@ -65,7 +65,7 @@ class DefaultRouteGenerator implements RouteGeneratorInterface
                 unset($parameters['id']);
             }
 
-            $parameters[$admin->getParent()->getIdParameter()] = $admin->getRequest()->get($admin->getParent()->getIdParameter());
+            $parameters[$admin->getParent()->getIdParameter()] = $admin->getRequest()->attributes->get($admin->getParent()->getIdParameter());
         }
 
         // if the admin is linked to a parent FieldDescription (ie, embedded widget)

+ 3 - 1
Tests/Route/DefaultRouteGeneratorTest.php

@@ -152,7 +152,9 @@ class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
         $parentAdmin->expects($this->never())->method('getPersistentParameters')->will($this->returnValue(array('from'=>'parent')));
 
         $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
-        $request->expects($this->any())
+        $request->attributes = $this->getMock('Symfony\Component\HttpFoundation\ParameterBag');
+        $request->attributes->expects($this->any())->method('has')->will($this->returnValue(true));
+        $request->attributes->expects($this->any())
             ->method('get')
             ->will($this->returnCallback(function($key) {
                 if ($key == 'childId') {