浏览代码

fix breadcrumbs generation

Thomas Rabaix 12 年之前
父节点
当前提交
b43ccc9c2b
共有 2 个文件被更改,包括 36 次插入34 次删除
  1. 29 27
      Admin/Admin.php
  2. 7 7
      Resources/views/standard_layout.html.twig

+ 29 - 27
Admin/Admin.php

@@ -1937,12 +1937,22 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
             return $this->getParent()->getBreadcrumbs($action);
             return $this->getParent()->getBreadcrumbs($action);
         }
         }
 
 
-        return $this->buildBreadcrumbs($action);
+        $menu = $this->buildBreadcrumbs($action);
+
+        do {
+            $breadcrumbs[] = $menu;
+        } while ($menu = $menu->getParent());
+
+        $breadcrumbs = array_reverse($breadcrumbs);
+        array_shift($breadcrumbs);
+        return $breadcrumbs;
     }
     }
 
 
     /**
     /**
      * Generates the breadcrumbs array
      * Generates the breadcrumbs array
      *
      *
+     * Note: the method will be called by the top admin instance (parent => child)
+     *
      * @param string                       $action
      * @param string                       $action
      * @param \Knp\Menu\ItemInterface|null $menu
      * @param \Knp\Menu\ItemInterface|null $menu
      *
      *
@@ -1956,14 +1966,14 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
 
 
         if (!$menu) {
         if (!$menu) {
             $menu = $this->menuFactory->createItem('root');
             $menu = $this->menuFactory->createItem('root');
-        }
 
 
-        $child = $menu->addChild(
-            $this->trans($this->getLabelTranslatorStrategy()->getLabel('dashboard', 'breadcrumb', 'link'), array(), 'SonataAdminBundle'),
-            array('uri' => $this->routeGenerator->generate('sonata_admin_dashboard'))
-        );
+            $menu = $menu->addChild(
+                $this->trans($this->getLabelTranslatorStrategy()->getLabel('dashboard', 'breadcrumb', 'link'), array(), 'SonataAdminBundle'),
+                array('uri' => $this->routeGenerator->generate('sonata_admin_dashboard'))
+            );
+        }
 
 
-        $child = $child->addChild(
+        $menu = $menu->addChild(
             $this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_list', $this->getClassnameLabel()), 'breadcrumb', 'link')),
             $this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_list', $this->getClassnameLabel()), 'breadcrumb', 'link')),
             array('uri' => $this->hasRoute('list') && $this->isGranted('LIST') ? $this->generateUrl('list') : null)
             array('uri' => $this->hasRoute('list') && $this->isGranted('LIST') ? $this->generateUrl('list') : null)
         );
         );
@@ -1973,45 +1983,37 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
         if ($childAdmin) {
         if ($childAdmin) {
             $id = $this->request->get($this->getIdParameter());
             $id = $this->request->get($this->getIdParameter());
 
 
-            $child = $child->addChild(
+            $menu = $menu->addChild(
                 $this->toString($this->getSubject()),
                 $this->toString($this->getSubject()),
                 array('uri' => $this->hasRoute('edit') && $this->isGranted('EDIT') ? $this->generateUrl('edit', array('id' => $id)) : null)
                 array('uri' => $this->hasRoute('edit') && $this->isGranted('EDIT') ? $this->generateUrl('edit', array('id' => $id)) : null)
             );
             );
 
 
-            return $childAdmin->buildBreadcrumbs($action, $child);
+            return $childAdmin->buildBreadcrumbs($action, $menu);
 
 
         } elseif ($this->isChild()) {
         } elseif ($this->isChild()) {
-            if ($action != 'list') {
-                $menu = $menu->addChild(
-                    $this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_list', $this->getClassnameLabel()), 'breadcrumb', 'link')),
-                    array('uri' => $this->hasRoute('list') && $this->isGranted('LIST') ? $this->generateUrl('list') : null)
-                );
-            }
 
 
-            if ($action != 'create' && $this->hasSubject()) {
-                $breadcrumbs = $menu->getBreadcrumbsArray($this->toString($this->getSubject()));
+            if ($action == 'list') {
+                $menu->setUri(false);
+            } elseif ($action != 'create' && $this->hasSubject()) {
+                $menu = $menu->addChild($this->toString($this->getSubject()));
             } else {
             } else {
-                $breadcrumbs = $menu->getBreadcrumbsArray(
+                $menu = $menu->addChild(
                     $this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_%s', $this->getClassnameLabel(), $action), 'breadcrumb', 'link'))
                     $this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_%s', $this->getClassnameLabel(), $action), 'breadcrumb', 'link'))
                 );
                 );
             }
             }
+
         } elseif ($action != 'list' && $this->hasSubject()) {
         } elseif ($action != 'list' && $this->hasSubject()) {
-            $breadcrumbs = $child->getBreadcrumbsArray(
-                $this->toString($this->getSubject())
-            );
+            $menu = $menu->addChild($this->toString($this->getSubject()));
         } elseif ($action != 'list') {
         } elseif ($action != 'list') {
-            $breadcrumbs = $child->getBreadcrumbsArray(
+            $menu = $menu->addChild(
 //                $this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_%s', $this->getClassnameLabel(), $action), 'breadcrumb', 'link'))
 //                $this->trans($this->getLabelTranslatorStrategy()->getLabel(sprintf('%s_%s', $this->getClassnameLabel(), $action), 'breadcrumb', 'link'))
                   $this->toString($this->getSubject())
                   $this->toString($this->getSubject())
             );
             );
         } else {
         } else {
-            $breadcrumbs = $child->getBreadcrumbsArray();
+            $menu->getBreadcrumbsArray();
         }
         }
 
 
-        // the generated $breadcrumbs contains an empty element
-        array_shift($breadcrumbs);
-
-        return $this->breadcrumbs[$action] = $breadcrumbs;
+        return $this->breadcrumbs[$action] = $menu;
     }
     }
 
 
     /**
     /**

+ 7 - 7
Resources/views/standard_layout.html.twig

@@ -56,11 +56,11 @@ file that was distributed with this source code.
             {% else %}
             {% else %}
                 {% if action is defined %}
                 {% if action is defined %}
                     -
                     -
-                    {% for label, uri in admin.breadcrumbs(action) %}
+                    {% for menu in admin.breadcrumbs(action) %}
                         {% if not loop.first  %}
                         {% if not loop.first  %}
                             >
                             >
                         {% endif %}
                         {% endif %}
-                        {{ label }}
+                        {{ menu.label }}
                     {% endfor %}
                     {% endfor %}
                 {% endif %}
                 {% endif %}
             {% endif%}
             {% endif%}
@@ -133,18 +133,18 @@ file that was distributed with this source code.
                         <ul class="breadcrumb">
                         <ul class="breadcrumb">
                             {% if _breadcrumb is empty %}
                             {% if _breadcrumb is empty %}
                                 {% if action is defined %}
                                 {% if action is defined %}
-                                    {% for label, uri in admin.breadcrumbs(action) %}
+                                    {% for menu in admin.breadcrumbs(action) %}
                                         {% if not loop.last  %}
                                         {% if not loop.last  %}
                                             <li>
                                             <li>
-                                                {% if uri is not empty %}
-                                                    <a href="{{ uri }}">{{ label }}</a>
+                                                {% if menu.uri is not empty %}
+                                                    <a href="{{ menu.uri }}">{{ menu.label }}</a>
                                                 {% else %}
                                                 {% else %}
-                                                    {{ label }}
+                                                    {{ menu.label }}
                                                 {% endif %}
                                                 {% endif %}
                                                 <span class="divider">/</span>
                                                 <span class="divider">/</span>
                                             </li>
                                             </li>
                                         {% else %}
                                         {% else %}
-                                            <li class="active">{{ label }}</li>
+                                            <li class="active">{{ menu.label }}</li>
                                         {% endif %}
                                         {% endif %}
                                     {% endfor %}
                                     {% endfor %}
                                 {% endif %}
                                 {% endif %}