5
5
use Sonata \AdminBundle \Form \FormMapper ;
6
6
use Sonata \DoctrinePHPCRAdminBundle \Admin \Admin ;
7
7
use Symfony \Cmf \Bundle \MenuBundle \Model \MenuNode ;
8
+ use Symfony \Cmf \Bundle \MenuBundle \Model \Menu ;
8
9
use Knp \Menu \ItemInterface as MenuItemInterface ;
9
10
use Doctrine \Common \Util \ClassUtils ;
10
11
11
12
class MenuNodeAdmin extends AbstractMenuNodeAdmin
12
13
{
13
14
protected $ baseRouteName = 'cmf_menu_menunode ' ;
14
15
protected $ baseRoutePattern = '/cmf/menu/menunode ' ;
16
+ protected $ recursiveBreadcrumbs = true ;
15
17
16
18
/**
17
19
* {@inheritDoc}
@@ -37,75 +39,39 @@ public function buildBreadcrumbs($action, MenuItemInterface $menu = null)
37
39
{
38
40
$ menuNodeNode = parent ::buildBreadcrumbs ($ action , $ menu );
39
41
40
- if ($ action != 'edit ' ) {
42
+ if ($ action != 'edit ' || ! $ this -> recursiveBreadcrumbs ) {
41
43
return $ menuNodeNode ;
42
44
}
43
45
44
- $ menuDoc = $ this ->getMenuForSubject ( $ this -> getSubject () );
46
+ $ parentDoc = $ this ->getSubject ()-> getParent ( );
45
47
$ pool = $ this ->getConfigurationPool ();
46
- $ menuAdmin = $ pool ->getAdminByClass (
47
- ClassUtils::getClass ($ menuDoc )
48
+ $ parentAdmin = $ pool ->getAdminByClass (
49
+ ClassUtils::getClass ($ parentDoc )
48
50
);
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 )
55
62
))
56
63
);
57
64
}
58
65
59
66
$ menuNodeNode ->setParent (null );
60
- $ current = $ menuEditNode ->addChild ($ menuNodeNode );
67
+ $ current = $ parentEditNode ->addChild ($ menuNodeNode );
61
68
62
69
return $ current ;
63
70
}
64
71
65
- protected function getMenuForSubject ( MenuNode $ subject )
72
+ public function setRecursiveBreadcrumbs ( $ recursiveBreadcrumbs )
66
73
{
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 ;
74
75
}
75
76
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
- }
111
77
}
0 commit comments