Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/1288' into PR_1288
Browse files Browse the repository at this point in the history
Conflicts:
	Tests/Admin/BaseAdminTest.php
  • Loading branch information
rande committed Jan 15, 2014
2 parents 96a7368 + 72c11bc commit 5dde52e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,8 @@ public function getFilterParameters()

// 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()));
}
}

Expand Down
43 changes: 43 additions & 0 deletions Resources/doc/reference/architecture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 30 additions & 0 deletions Tests/Admin/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1166,4 +1166,34 @@ public function testRemoveFieldFromFormGroup()
$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']);
}
}
7 changes: 6 additions & 1 deletion Tests/Fixtures/Admin/CommentAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public function configureRoutes(RouteCollection $collection)
{
$collection->remove('edit');
}
}

public function setParentAssociationMapping($associationMapping)
{
$this->parentAssociationMapping = $associationMapping;
}
}
3 changes: 1 addition & 2 deletions Tests/Fixtures/Admin/PostAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ public function getClassMetaData()

return parent::getClassMetaData();
}
}

}

0 comments on commit 5dde52e

Please sign in to comment.