55use Sonata \AdminBundle \Form \FormMapper ;
66use Sonata \DoctrinePHPCRAdminBundle \Admin \Admin ;
77use Symfony \Cmf \Bundle \MenuBundle \Model \MenuNode ;
8+ use Symfony \Cmf \Bundle \MenuBundle \Model \Menu ;
89use Knp \Menu \ItemInterface as MenuItemInterface ;
910use Doctrine \Common \Util \ClassUtils ;
1011
1112class 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}
0 commit comments