Browse Source

Merge remote-tracking branch 'origin/pr/1288' into PR_1288

Conflicts:
	Tests/Admin/BaseAdminTest.php
Thomas Rabaix 11 years ago
parent
commit
5dde52e96b

+ 2 - 1
Admin/Admin.php

@@ -777,7 +777,8 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
 
             // always force the parent value
             if ($this->isChild() && $this->getParentAssociationMapping()) {
-                $parameters[$this->getParentAssociationMapping()] = array('value' => $this->request->get($this->getParent()->getIdParameter()));
+                $name = str_replace('.', '__', $this->getParentAssociationMapping());
+                $parameters[$name] = array('value' => $this->request->get($this->getParent()->getIdParameter()));
             }
         }
 

+ 43 - 0
Resources/doc/reference/architecture.rst

@@ -271,5 +271,48 @@ template files, administration panel title and logo.
 
 
 
+Create child admins
+-------------------
+
+Let's say you have a PostAdmin and a CommentAdmin. You can optionally declare the CommentAdmin
+to be a child of the PostAdmin. This will create new routes like, for example, ``/post/{id}/comment/list``,
+where the comments will automatically be filtered by post.
+
+To do this, you first need to call the ``addChild`` method in your PostAdmin service configuration :
+
+.. code-block:: xml
+
+    <!-- app/config/config.xml -->
+    <service id="sonata.news.admin.post" class="Sonata\NewsBundle\Admin\CommentAdmin">
+        ...
+
+        <call method="addChild">
+            <argument type="service" id="sonata.news.admin.comment" />
+        </call>
+    </service>
+
+Then, you have to set the CommentAdmin ``parentAssociationMapping`` attribute to ``post`` :
+
+.. code-block:: php
+
+    <?php
+    namespace Sonata\NewsBundle\Admin;
+
+    ...
+
+    class CommentAdmin extends Admin
+    {
+        protected $parentAssociationMapping = 'post';
+
+        // OR
+
+        public function getParentAssociationMapping()
+        {
+            return 'post';
+        }
+    }
+
+It also possible to set a dot-separated value, like ``post.author``, if your parent and child admins are not directly related.
+
 .. _`Django Project Website`: http://www.djangoproject.com/
 .. _`CRUD is an acronym`: http://en.wikipedia.org/wiki/CRUD

+ 30 - 0
Tests/Admin/AdminTest.php

@@ -1166,4 +1166,34 @@ class AdminTest extends \PHPUnit_Framework_TestCase
         $admin->removeFieldFromFormGroup('bar');
         $this->assertEquals($admin->getFormGroups(), array());
     }
+
+    public function testGetFilterParameters()
+    {
+        $authorId = uniqid();
+
+        $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
+
+        $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
+        $commentAdmin->setParentAssociationMapping('post.author');
+        $commentAdmin->setParent($postAdmin);
+
+        $request = $this->getMock('Symfony\Component\HttpFoundation\Request', array('get'));
+        $request->expects($this->any())
+            ->method('get')
+            ->will($this->returnValue($authorId));
+
+        $commentAdmin->setRequest($request);
+
+        $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
+        $modelManager->expects($this->any())
+            ->method('getDefaultSortValues')
+            ->will($this->returnValue(array()));
+
+        $commentAdmin->setModelManager($modelManager);
+
+        $parameters = $commentAdmin->getFilterParameters();
+
+        $this->assertTrue(isset($parameters['post__author']));
+        $this->assertEquals(array('value' => $authorId), $parameters['post__author']);
+    }
 }

+ 6 - 1
Tests/Fixtures/Admin/CommentAdmin.php

@@ -20,4 +20,9 @@ class CommentAdmin extends Admin
     {
         $collection->remove('edit');
     }
-}
+
+    public function setParentAssociationMapping($associationMapping)
+    {
+        $this->parentAssociationMapping = $associationMapping;
+    }
+}

+ 1 - 2
Tests/Fixtures/Admin/PostAdmin.php

@@ -26,5 +26,4 @@ class PostAdmin extends Admin
 
         return parent::getClassMetaData();
     }
-}
-
+}