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

Commit d7ea06a

Browse files
committed
Merge pull request #135 from benglass/master
Simplify breadcrumb code to allow for non-Menu MenuNode parents.
2 parents 0653ec9 + d09249a commit d7ea06a

File tree

5 files changed

+26
-122
lines changed

5 files changed

+26
-122
lines changed

Admin/MenuNodeAdmin.php

Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
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

1112
class MenuNodeAdmin extends AbstractMenuNodeAdmin
1213
{
1314
protected $baseRouteName = 'cmf_menu_menunode';
1415
protected $baseRoutePattern = '/cmf/menu/menunode';
16+
protected $recursiveBreadcrumbs = true;
1517

1618
/**
1719
* {@inheritDoc}
@@ -37,75 +39,39 @@ public function buildBreadcrumbs($action, MenuItemInterface $menu = null)
3739
{
3840
$menuNodeNode = parent::buildBreadcrumbs($action, $menu);
3941

40-
if ($action != 'edit') {
42+
if ($action != 'edit' || ! $this->recursiveBreadcrumbs) {
4143
return $menuNodeNode;
4244
}
4345

44-
$menuDoc = $this->getMenuForSubject($this->getSubject());
46+
$parentDoc = $this->getSubject()->getParent();
4547
$pool = $this->getConfigurationPool();
46-
$menuAdmin = $pool->getAdminByClass(
47-
ClassUtils::getClass($menuDoc)
48+
$parentAdmin = $pool->getAdminByClass(
49+
ClassUtils::getClass($parentDoc)
4850
);
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)
51+
52+
if (null === $parentAdmin) {
53+
return $menuNodeNode;
54+
}
55+
56+
$parentAdmin->setSubject($parentDoc);
57+
$parentEditNode = $parentAdmin->buildBreadcrumbs($action, $menu);
58+
if ($parentAdmin->isGranted('EDIT' && $parentAdmin->hasRoute('edit'))) {
59+
$parentEditNode->setUri(
60+
$parentAdmin->generateUrl('edit', array(
61+
'id' => $this->getUrlsafeIdentifier($parentDoc)
5562
))
5663
);
5764
}
5865

5966
$menuNodeNode->setParent(null);
60-
$current = $menuEditNode->addChild($menuNodeNode);
67+
$current = $parentEditNode->addChild($menuNodeNode);
6168

6269
return $current;
6370
}
6471

65-
protected function getMenuForSubject(MenuNode $subject)
72+
public function setRecursiveBreadcrumbs($recursiveBreadcrumbs)
6673
{
67-
$id = $subject->getId();
68-
69-
$menuId = $this->getMenuIdForNodeId($id);
70-
71-
$menu = $this->modelManager->find(null, $menuId);
72-
73-
return $menu;
74+
$this->recursiveBreadcrumbs = (bool) $recursiveBreadcrumbs;
7475
}
7576

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-
}
11177
}

DependencyInjection/CmfMenuExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function loadPhpcr($config, XmlFileLoader $loader, ContainerBuilder $cont
6262
'menu_basepath' => 'menu_basepath',
6363
'content_basepath' => 'content_basepath',
6464
'manager_name' => 'manager_name',
65+
'admin_recursive_breadcrumbs' => 'admin_recursive_breadcrumbs',
6566
);
6667

6768
foreach ($keys as $sourceKey => $targetKey) {

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function getConfigTreeBuilder()
3434
->scalarNode('menu_admin_class')->defaultNull()->end()
3535
->scalarNode('node_admin_class')->defaultNull()->end()
3636
->scalarNode('content_basepath')->defaultNull()->end()
37+
->scalarNode('admin_recursive_breadcrumbs')->defaultTrue()->end()
3738
->end()
3839
->end()
3940
->end()

Resources/config/admin.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@
7474
<argument type="service" id="cmf_menu.factory"/>>
7575
</call>
7676

77+
<call method="setRecursiveBreadcrumbs">
78+
<argument>%cmf_menu.persistence.phpcr.admin_recursive_breadcrumbs%</argument>
79+
</call>
80+
7781
</service>
7882

7983
<service id="cmf_menu.admin_extension.menu_node_referrers" class="%cmf_menu.admin_extension.menu_reference.class%">

Tests/Unit/Admin/MenuNodeAdminTest.php

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

0 commit comments

Comments
 (0)