Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 904a93d

Browse files
author
Ben Glassman
committed
Simplify breadcrumb code to allow for non-Menu MenuNode parents.
1 parent 00b2031 commit 904a93d

File tree

2 files changed

+16
-125
lines changed

2 files changed

+16
-125
lines changed

Admin/MenuNodeAdmin.php

Lines changed: 16 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Sonata\AdminBundle\Form\FormMapper;
66
use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;
77
use Symfony\Cmf\Bundle\MenuBundle\Model\MenuNode;
8+
use Symfony\Cmf\Bundle\MenuBundle\Model\Menu;
89
use Knp\Menu\ItemInterface as MenuItemInterface;
910
use Doctrine\Common\Util\ClassUtils;
1011

@@ -41,71 +42,29 @@ public function buildBreadcrumbs($action, MenuItemInterface $menu = null)
4142
return $menuNodeNode;
4243
}
4344

44-
$menuDoc = $this->getMenuForSubject($this->getSubject());
45+
$parentDoc = $this->getSubject()->getParent();
4546
$pool = $this->getConfigurationPool();
46-
$menuAdmin = $pool->getAdminByClass(
47-
ClassUtils::getClass($menuDoc)
47+
$parentAdmin = $pool->getAdminByClass(
48+
ClassUtils::getClass($parentDoc)
4849
);
49-
$menuAdmin->setSubject($menuDoc);
50-
$menuEditNode = $menuAdmin->buildBreadcrumbs($action, $menu);
51-
if ($menuAdmin->isGranted('EDIT' && $menuAdmin->hasRoute('edit'))) {
52-
$menuEditNode->setUri(
53-
$menuAdmin->generateUrl('edit', array(
54-
'id' => $this->getUrlsafeIdentifier($menuDoc)
50+
51+
if (null === $parentAdmin) {
52+
return $menuNodeNode;
53+
}
54+
55+
$parentAdmin->setSubject($parentDoc);
56+
$parentEditNode = $parentAdmin->buildBreadcrumbs($action, $menu);
57+
if ($parentAdmin->isGranted('EDIT' && $parentAdmin->hasRoute('edit'))) {
58+
$parentEditNode->setUri(
59+
$parentAdmin->generateUrl('edit', array(
60+
'id' => $this->getUrlsafeIdentifier($parentDoc)
5561
))
5662
);
5763
}
5864

5965
$menuNodeNode->setParent(null);
60-
$current = $menuEditNode->addChild($menuNodeNode);
66+
$current = $parentEditNode->addChild($menuNodeNode);
6167

6268
return $current;
6369
}
64-
65-
protected function getMenuForSubject(MenuNode $subject)
66-
{
67-
$id = $subject->getId();
68-
69-
$menuId = $this->getMenuIdForNodeId($id);
70-
71-
$menu = $this->modelManager->find(null, $menuId);
72-
73-
return $menu;
74-
}
75-
76-
protected function getMenuIdForNodeId($id)
77-
{
78-
// I wonder if this could be simplified in Phpcr/PathHelper
79-
//
80-
// $relPath = PathHelper::removeBasePath($this->menuRoot, $id);
81-
// $menuId = PathHelper:splicePath($relPath, 0, 1);
82-
83-
if (0 !== strpos($id, $this->menuRoot)) {
84-
throw new \InvalidArgumentException(sprintf(
85-
'Cannot find base path "%s" in menu node ID "%s"', $this->menuRoot, $id
86-
));
87-
}
88-
89-
$relPath = substr($id, strlen($this->menuRoot) + 1);
90-
$parts = explode('/', $relPath);
91-
92-
if (count($parts) == 0) {
93-
throw new \InvalidArgumentException(sprintf(
94-
'ID for menu node "%s" may not be the same as root path "%s"',
95-
$id, $this->menuRoot
96-
));
97-
}
98-
99-
if (count($parts) == 1) {
100-
throw new \InvalidArgumentException(sprintf(
101-
'MenuNode "%s" may not hold the position reserved for a Menu.',
102-
$id
103-
));
104-
}
105-
106-
$first = $parts[0];
107-
$menuId = sprintf('%s/%s', $this->menuRoot, $first);
108-
109-
return $menuId;
110-
}
11170
}

Tests/Unit/Admin/MenuNodeAdminTest.php

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)