Prechádzať zdrojové kódy

improve request management with nested admin

Thomas 14 rokov pred
rodič
commit
4cae6e22e1
2 zmenil súbory, kde vykonal 26 pridanie a 4 odobranie
  1. 22 3
      Admin/Admin.php
  2. 4 1
      Controller/CRUDController.php

+ 22 - 3
Admin/Admin.php

@@ -746,9 +746,11 @@ abstract class Admin implements AdminInterface
             $parameters['code']   = $this->getCode();
         }
 
-        // allows to define persistent parameters 
-        $parameters = array_merge($this->getPersistentParameters(), $parameters);
-
+        // allows to define persistent parameters
+        if ($this->hasRequest()) {
+            $parameters = array_merge($this->getPersistentParameters(), $parameters);
+        }
+        
         $route = $this->getRoute($name);
 
         if (!$route) {
@@ -1600,6 +1602,10 @@ abstract class Admin implements AdminInterface
         if ($request->get('uniqid')) {
             $this->setUniqid($request->get('uniqid'));
         }
+
+        foreach ($this->getChildren() as $children) {
+            $children->setRequest($request);
+        }
     }
 
     /**
@@ -1607,9 +1613,22 @@ abstract class Admin implements AdminInterface
      */
     public function getRequest()
     {
+        if (!$this->request) {
+            throw new \RuntimeException('The Request object has not been set');
+        }
+        
         return $this->request;
     }
 
+    /**
+     *
+     * @return true if the request object is linked to the Admin
+     */
+    public function hasRequest()
+    {
+        return $this->request !== null;
+    }
+
     /**
      * @param \Sonata\AdminBundle\Builder\FormBuilderInterface $formBuilder
      * @return void

+ 4 - 1
Controller/CRUDController.php

@@ -94,11 +94,14 @@ class CRUDController extends Controller
             throw new \RuntimeException(sprintf('Unable to find the admin class related to the current controller (%s)', get_class($this)));
         }
 
-        $this->admin->setRequest($this->container->get('request'));
+        $rootAdmin = $this->admin;
 
         if ($this->admin->isChild()) {
             $this->admin->setCurrentChild(true);
+            $rootAdmin = $rootAdmin->getParent();
         }
+
+        $rootAdmin->setRequest($this->container->get('request'));
     }
 
     /**