فهرست منبع

Remove the 'getBreadcrumb' from the CRUDController

Thomas 14 سال پیش
والد
کامیت
e3edb9c1c4
3فایلهای تغییر یافته به همراه69 افزوده شده و 52 حذف شده
  1. 64 35
      Admin/Admin.php
  2. 4 17
      Controller/CRUDController.php
  3. 1 0
      Twig/Extension/SonataAdminExtension.php

+ 64 - 35
Admin/Admin.php

@@ -270,6 +270,13 @@ abstract class Admin implements AdminInterface
      */
     protected $router;
 
+    /**
+     * The generated breadcrumbs
+     *
+     * @var array
+     */
+    protected $breadcrumbs = array();
+
     /**
      * The configuration pool
      * 
@@ -316,6 +323,10 @@ abstract class Admin implements AdminInterface
     {
     }
 
+    /**
+     *
+     * @param DatagridMapper
+     */
     protected function configureDatagridFilters(DatagridMapper $filter)
     {
     }
@@ -1438,6 +1449,20 @@ abstract class Admin implements AdminInterface
         return array();
     }
 
+    /**
+     * @param string $action
+     * @return array
+     */
+    public function getBreadcrumbs($action)
+    {
+
+        if ($this->isChild()) {
+            return $this->getParent()->getBreadcrumbs($action);
+        }
+
+        return $this->buildBreadcrumbs($action);
+    }
+
     /**
      * generate the breadcrumbs array
      *
@@ -1445,55 +1470,59 @@ abstract class Admin implements AdminInterface
      * @param \Knplabs\MenuBundle\MenuItem|null $menu
      * @return array the breadcrumbs
      */
-    public function getBreadcrumbs($action, MenuItem $menu = null)
+    public function buildBreadcrumbs($action, MenuItem $menu = null)
     {
-        $menu = $menu ?: new Menu;
+        if (!isset($this->breadcrumbs[$action])) {
+            $menu = $menu ?: new Menu;
 
-        $child = $menu->addChild(
-            $this->trans(sprintf('link_%s_list', $this->getClassnameLabel())),
-            $this->generateUrl('list')
-        );
-        
-        $childAdmin = $this->getCurrentChildAdmin();
+            $child = $menu->addChild(
+                $this->trans(sprintf('link_%s_list', $this->getClassnameLabel())),
+                $this->generateUrl('list')
+            );
 
-        if ($childAdmin) {
-            $id = $this->request->get($this->getIdParameter());
+            $childAdmin = $this->getCurrentChildAdmin();
 
-            $child = $child->addChild(
-                (string) $this->getSubject(),
-                $this->generateUrl('edit', array('id' => $id))
-            );
+            if ($childAdmin) {
+                $id = $this->request->get($this->getIdParameter());
 
-            return $childAdmin->getBreadcrumbs($action, $child);
-        
-        } elseif ($this->isChild()) {
+                $child = $child->addChild(
+                    (string) $this->getSubject(),
+                    $this->generateUrl('edit', array('id' => $id))
+                );
+
+                return $childAdmin->buildBreadcrumbs($action, $child);
+
+            } elseif ($this->isChild()) {
+
+                if ($action != 'list') {
+                    $menu = $menu->addChild(
+                        $this->trans(sprintf('link_%s_list', $this->getClassnameLabel())),
+                        $this->generateUrl('list')
+                    );
+                }
 
-            if ($action != 'list') {
-                $menu = $menu->addChild(
-                    $this->trans(sprintf('link_%s_list', $this->getClassnameLabel())),
-                    $this->generateUrl('list')
+                $breadcrumbs = $menu->getBreadcrumbsArray(
+                    $this->trans(sprintf('link_%s_%s', $this->getClassnameLabel(), $action))
                 );
-            }
 
-            $breadcrumbs = $menu->getBreadcrumbsArray(
-                $this->trans(sprintf('link_%s_%s', $this->getClassnameLabel(), $action))
-            );
+            } else if ($action != 'list') {
 
-        } else if ($action != 'list') {
+                $breadcrumbs = $child->getBreadcrumbsArray(
+                    $this->trans(sprintf('link_%s_%s', $this->getClassnameLabel(), $action))
+                );
 
-            $breadcrumbs = $child->getBreadcrumbsArray(
-                $this->trans(sprintf('link_%s_%s', $this->getClassnameLabel(), $action))
-            );
+            } else {
+
+                $breadcrumbs = $child->getBreadcrumbsArray();
+            }
 
-        } else {
+            // the generated $breadcrumbs contains an empty element
+            array_shift($breadcrumbs);
 
-            $breadcrumbs = $child->getBreadcrumbsArray();
+            $this->breadcrumbs[$action] = $breadcrumbs;
         }
 
-        // the generated $breadcrumbs contains an empty element
-        array_shift($breadcrumbs);
-
-        return $breadcrumbs;
+        return $this->breadcrumbs[$action];
     }
 
     /**

+ 4 - 17
Controller/CRUDController.php

@@ -132,7 +132,7 @@ class CRUDController extends Controller
             'admin'             => $this->admin,
             'base_template'     => $this->getBaseTemplate(),
             'side_menu'         => $this->getSideMenu('list'),
-            'breadcrumbs'       => $this->getBreadcrumbs('list'),
+            'breadcrumbs'       => $this->admin->getBreadcrumbs('list'),
         ));
     }
 
@@ -225,7 +225,7 @@ class CRUDController extends Controller
             'admin'          => $this->admin,
             'base_template'  => $this->getBaseTemplate(),
             'side_menu'      => $this->getSideMenu('edit'),
-            'breadcrumbs'    => $this->getBreadcrumbs('edit'),
+            'breadcrumbs'    => $this->admin->getBreadcrumbs('edit'),
         ));
     }
 
@@ -322,7 +322,7 @@ class CRUDController extends Controller
             'admin'         => $this->admin,
             'base_template' => $this->getBaseTemplate(),
             'side_menu'     => $this->getSideMenu('create'),
-            'breadcrumbs'   => $this->getBreadcrumbs('create'),
+            'breadcrumbs'   => $this->admin->getBreadcrumbs('create'),
         ));
     }
 
@@ -332,6 +332,7 @@ class CRUDController extends Controller
      */
     public function getSideMenu($action)
     {
+        
         if ($this->admin->isChild()) {
             return $this->admin->getParent()->getSideMenu($action, $this->admin);
         }
@@ -339,18 +340,4 @@ class CRUDController extends Controller
         return $this->admin->getSideMenu($action);
     }
 
-    /**
-     * @param string $action
-     * @return array
-     */
-    public function getBreadcrumbs($action)
-    {
-        
-        if ($this->admin->isChild()) {
-            return $this->admin->getParent()->getBreadcrumbs($action);
-        }
-
-        return $this->admin->getBreadcrumbs($action);
-    }
-
 }

+ 1 - 0
Twig/Extension/SonataAdminExtension.php

@@ -45,6 +45,7 @@ class SonataAdminExtension extends \Twig_Extension
     {
 
         return array(
+
         );
     }