瀏覽代碼

add AdminInterface::isCurrentRoute method

Check the current request's name, if matched with given admin's route,
will return true. This is useful when share configureFormFields method's
results between create/edit/custom action, you can build different rule
form for different page.
Rivsen Tan 9 年之前
父節點
當前提交
9a80e04b3c
共有 4 個文件被更改,包括 80 次插入2 次删除
  1. 25 0
      Admin/Admin.php
  2. 17 0
      Admin/AdminInterface.php
  3. 8 2
      Resources/doc/cookbook/recipe_dynamic_form_modification.rst
  4. 30 0
      Tests/Admin/AdminTest.php

+ 25 - 0
Admin/Admin.php

@@ -1304,6 +1304,31 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
         return $this->routeGenerator->hasAdminRoute($this, $name);
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function isCurrentRoute($name, $adminCode = null)
+    {
+        if (!$this->hasRequest()) {
+            return false;
+        }
+
+        $request = $this->getRequest();
+        $route = $request->get('_route');
+
+        if ($adminCode) {
+            $admin = $this->getConfigurationPool()->getAdminByAdminCode($adminCode);
+        } else {
+            $admin = $this;
+        }
+
+        if (!$admin) {
+            return false;
+        }
+
+        return ($admin->getBaseRouteName().'_'.$name) == $route;
+    }
+
     /**
      * {@inheritdoc}
      */

+ 17 - 0
Admin/AdminInterface.php

@@ -307,6 +307,23 @@ interface AdminInterface
      */
     public function hasRoute($name);
 
+    /**
+     * Check the current request is given route or not.
+     *
+     * TODO: uncomment this method before releasing 4.0
+     *
+     * ```
+     * $this->isCurrentRoute('create'); // is create page?
+     * $this->isCurrentRoute('edit', 'some.admin.code'); // is some.admin.code admin's edit page?
+     * ```
+     *
+     * @param string $name
+     * @param string $adminCode
+     *
+     * @return bool
+     */
+    // public function isCurrentRoute($name, $adminCode = null);
+
     /**
      * Returns true if the admin has a FieldDescription with the given $name.
      *

+ 8 - 2
Resources/doc/cookbook/recipe_dynamic_form_modification.rst

@@ -10,7 +10,8 @@ This is a way for you to accomplish this.
 
 In your ``Admin`` class' ``configureFormFields`` method you are able to get the
 current object by calling ``$this->getSubject()``. The value returned will be your
-linked model. Then, you should be able to dynamically add needed fields to the form:
+linked model. And another method ``isCurrentRoute`` for check the current request's route.
+Then, you should be able to dynamically add needed fields to the form:
 
 .. code-block:: php
 
@@ -22,7 +23,7 @@ linked model. Then, you should be able to dynamically add needed fields to the f
     use Sonata\AdminBundle\Admin\Admin;
     use Sonata\AdminBundle\Form\FormMapper;
 
-    classPostAdmin extends Admin
+    class PostAdmin extends Admin
     {
         // ...
 
@@ -39,5 +40,10 @@ linked model. Then, you should be able to dynamically add needed fields to the f
                 // The thumbnail field will only be added when the edited item is created
                 $formMapper->add('thumbnail', 'file');
             }
+
+            // Name field will be added only when create an item
+            if ($this->isCurrentRoute('create')) {
+                $formMapper->add('name', 'text');
+            }
         }
     }

+ 30 - 0
Tests/Admin/AdminTest.php

@@ -13,6 +13,7 @@ namespace Sonata\AdminBundle\Tests\Admin;
 
 use Sonata\AdminBundle\Admin\Admin;
 use Sonata\AdminBundle\Admin\AdminInterface;
+use Sonata\AdminBundle\Admin\Pool;
 use Sonata\AdminBundle\Route\DefaultRouteGenerator;
 use Sonata\AdminBundle\Route\RoutesCache;
 use Sonata\AdminBundle\Tests\Fixtures\Admin\CommentAdmin;
@@ -25,6 +26,7 @@ use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Post;
 use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Tag;
 use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToString;
 use Sonata\AdminBundle\Tests\Fixtures\Entity\FooToStringNull;
+use Symfony\Component\DependencyInjection\Container;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\PropertyAccess\PropertyAccess;
 
@@ -704,18 +706,26 @@ class AdminTest extends \PHPUnit_Framework_TestCase
             new RoutesCache($this->cacheTempFolder, true)
         );
 
+        $container = new Container();
+        $pool = new Pool($container, 'Sonata Admin', '/path/to/pic.png');
+
         $pathInfo = new \Sonata\AdminBundle\Route\PathInfoBuilder($this->getMock('Sonata\AdminBundle\Model\AuditManagerInterface'));
         $postAdmin = new PostAdmin('sonata.post.admin.post', $objFqn, 'SonataNewsBundle:PostAdmin');
+        $container->set('sonata.post.admin.post', $postAdmin);
+        $postAdmin->setConfigurationPool($pool);
         $postAdmin->setRouteBuilder($pathInfo);
         $postAdmin->setRouteGenerator($routeGenerator);
         $postAdmin->initialize();
 
         $commentAdmin = new CommentAdmin('sonata.post.admin.comment', 'Application\Sonata\NewsBundle\Entity\Comment', 'SonataNewsBundle:CommentAdmin');
+        $container->set('sonata.post.admin.comment', $commentAdmin);
+        $commentAdmin->setConfigurationPool($pool);
         $commentAdmin->setRouteBuilder($pathInfo);
         $commentAdmin->setRouteGenerator($routeGenerator);
         $commentAdmin->initialize();
 
         $postAdmin->addChild($commentAdmin);
+        $pool->setAdminServiceIds(array('sonata.post.admin.post', 'sonata.post.admin.comment'));
 
         $this->assertSame($expected.'_comment', $commentAdmin->getBaseRouteName());
 
@@ -725,6 +735,26 @@ class AdminTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($postAdmin->hasRoute('sonata.post.admin.comment.list'));
         $this->assertFalse($postAdmin->hasRoute('sonata.post.admin.post|sonata.post.admin.comment.edit'));
         $this->assertFalse($commentAdmin->hasRoute('edit'));
+
+        /*
+         * Test the route name from request
+         */
+        $postListRequest = new Request(
+            array(),
+            array(),
+            array(
+                '_route' => $postAdmin->getBaseRouteName().'_list',
+            )
+        );
+
+        $postAdmin->setRequest($postListRequest);
+        $commentAdmin->setRequest($postListRequest);
+
+        $this->assertTrue($postAdmin->isCurrentRoute('list'));
+        $this->assertFalse($postAdmin->isCurrentRoute('create'));
+        $this->assertFalse($commentAdmin->isCurrentRoute('list'));
+        $this->assertTrue($commentAdmin->isCurrentRoute('list', 'sonata.post.admin.post'));
+        $this->assertFalse($commentAdmin->isCurrentRoute('edit', 'sonata.post.admin.post'));
     }
 
     /**