فهرست منبع

Merge pull request #4632 from core23/docs-admin

Document how to display a child admin
Grégoire Paris 7 سال پیش
والد
کامیت
3b0357b3d9
1فایلهای تغییر یافته به همراه52 افزوده شده و 11 حذف شده
  1. 52 11
      Resources/doc/reference/architecture.rst

+ 52 - 11
Resources/doc/reference/architecture.rst

@@ -316,43 +316,83 @@ template files, administration panel title and logo.
 Create child admins
 -------------------
 
-Let us 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.
+Let us say you have a ``PlaylistAdmin`` and a ``VideoAdmin``. You can optionally declare the ``VideoAdmin``
+to be a child of the ``PlaylistAdmin``. This will create new routes like, for example, ``/playlist/{id}/video/list``,
+where the videos will automatically be filtered by post.
 
-To do this, you first need to call the ``addChild`` method in your PostAdmin service configuration :
+To do this, you first need to call the ``addChild`` method in your ``PlaylistAdmin`` service configuration:
 
 .. configuration-block::
 
     .. code-block:: xml
 
         <!-- app/config/config.xml -->
-        <service id="sonata.news.admin.post" class="Sonata\NewsBundle\Admin\PostAdmin">
+        <service id="sonata.admin.playlist" class="AppBundle\Admin\PlaylistAdmin">
             <!-- ... -->
 
             <call method="addChild">
-                <argument type="service" id="sonata.news.admin.comment" />
+                <argument type="service" id="sonata.admin.video" />
             </call>
         </service>
 
-Then, you have to set the CommentAdmin ``parentAssociationMapping`` attribute to ``post`` :
+Then, you have to set the VideoAdmin ``parentAssociationMapping`` attribute to ``playlist`` :
 
 .. code-block:: php
 
     <?php
-    namespace Sonata\NewsBundle\Admin;
+
+    namespace AppBundle\Admin;
 
     // ...
 
-    class CommentAdmin extends AbstractAdmin
+    class VideoAdmin extends AbstractAdmin
     {
-        protected $parentAssociationMapping = 'post';
+        protected $parentAssociationMapping = 'playlist';
 
         // OR
 
         public function getParentAssociationMapping()
         {
-            return 'post';
+            return 'playlist';
+        }
+    }
+
+To display the ``VideoAdmin`` extend the menu in your ``PlaylistAdmin`` class:
+
+.. code-block:: php
+
+    <?php
+
+    namespace AppBundle\Admin;
+
+    use Knp\Menu\ItemInterface as MenuItemInterface;
+    use Sonata\AdminBundle\Admin\AbstractAdmin;
+    use Sonata\AdminBundle\Admin\AdminInterface;
+
+    class PlaylistAdmin extends AbstractAdmin
+    {
+        // ...
+
+        protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
+        {
+            if (!$childAdmin && !in_array($action, array('edit', 'show'))) {
+                return;
+            }
+
+            $admin = $this->isChild() ? $this->getParent() : $this;
+            $id = $admin->getRequest()->get('id');
+
+            $menu->addChild('View Playlist', array('uri' => $admin->generateUrl('show', array('id' => $id))));
+
+            if ($this->isGranted('EDIT')) {
+                $menu->addChild('Edit Playlist', array('uri' => $admin->generateUrl('edit', array('id' => $id))));
+            }
+
+            if ($this->isGranted('LIST')) {
+                $menu->addChild('Manage Videos', array(
+                    'uri' => $admin->generateUrl('sonata.admin.video.list', array('id' => $id))
+                ));
+            }
         }
     }
 
@@ -365,6 +405,7 @@ of them, you may override the ``configureRoutes`` method::
     <?php
     namespace Sonata\NewsBundle\Admin;
 
+    use Sonata\AdminBundle\Admin\AbstractAdmin;
     use Sonata\AdminBundle\Route\RouteCollection;
 
     class CommentAdmin extends AbstractAdmin