9
9
* file that was distributed with this source code.
10
10
*/
11
11
12
-
13
12
namespace Symfony \Cmf \Bundle \MenuBundle ;
14
13
15
14
use Knp \Menu \Silex \RouterAwareFactory ;
16
15
use Knp \Menu \ItemInterface ;
17
16
use Knp \Menu \NodeInterface ;
18
17
use Knp \Menu \MenuItem ;
19
18
19
+ use Psr \Log \LoggerInterface ;
20
+
20
21
use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
21
22
use Symfony \Component \Routing \Exception \RouteNotFoundException ;
22
- use Symfony \Component \Security \Core \SecurityContextInterface ;
23
- use Symfony \Cmf \Bundle \CoreBundle \PublishWorkflow \PublishWorkflowChecker ;
24
-
25
- use Psr \Log \LoggerInterface ;
26
23
27
24
use Symfony \Cmf \Bundle \MenuBundle \Voter \VoterInterface ;
25
+ use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
26
+ use Symfony \Cmf \Bundle \MenuBundle \Event \CreateMenuItemFromNodeEvent ;
28
27
29
28
/**
30
29
* This factory builds menu items from the menu nodes and builds urls based on
@@ -60,18 +59,6 @@ class ContentAwareFactory extends RouterAwareFactory
60
59
*/
61
60
private $ logger ;
62
61
63
- /**
64
- * @var SecurityContextInterface
65
- */
66
- private $ securityContext ;
67
-
68
- /**
69
- * The permission to check for when doing the publish workflow check.
70
- *
71
- * @var string
72
- */
73
- private $ publishWorkflowPermission = PublishWorkflowChecker::VIEW_ATTRIBUTE ;
74
-
75
62
/**
76
63
* Whether to return null or a MenuItem without any URL if no URL can be
77
64
* found for a MenuNode.
@@ -84,22 +71,20 @@ class ContentAwareFactory extends RouterAwareFactory
84
71
* @param UrlGeneratorInterface $generator for the parent class
85
72
* @param UrlGeneratorInterface $contentRouter to generate routes when
86
73
* content is set
87
- * @param SecurityContextInterface $securityContext the publish workflow
88
- * checker to check if menu items are published.
89
74
* @param LoggerInterface $logger
90
75
*/
91
76
public function __construct (
92
77
UrlGeneratorInterface $ generator ,
93
78
UrlGeneratorInterface $ contentRouter ,
94
- SecurityContextInterface $ securityContext ,
95
- LoggerInterface $ logger
79
+ LoggerInterface $ logger ,
80
+ EventDispatcherInterface $ dispatcher
96
81
)
97
82
{
98
83
parent ::__construct ($ generator );
99
84
$ this ->contentRouter = $ contentRouter ;
100
- $ this ->securityContext = $ securityContext ;
101
85
$ this ->logger = $ logger ;
102
86
$ this ->linkTypes = array ('route ' , 'uri ' , 'content ' );
87
+ $ this ->dispatcher = $ dispatcher ;
103
88
}
104
89
105
90
/**
@@ -124,17 +109,6 @@ public function setAllowEmptyItems($allowEmptyItems)
124
109
$ this ->allowEmptyItems = $ allowEmptyItems ;
125
110
}
126
111
127
- /**
128
- * What attribute to use in the publish workflow check. This typically
129
- * is VIEW or VIEW_ANONYMOUS.
130
- *
131
- * @param string $attribute
132
- */
133
- public function setPublishWorkflowPermission ($ attribute )
134
- {
135
- $ this ->publishWorkflowPermission = $ attribute ;
136
- }
137
-
138
112
/**
139
113
* Add a voter to decide on current item.
140
114
*
@@ -169,17 +143,36 @@ private function getVoters()
169
143
*/
170
144
public function createFromNode (NodeInterface $ node )
171
145
{
172
- $ item = $ this ->createItem ($ node ->getName (), $ node ->getOptions ());
146
+ $ event = new CreateMenuItemFromNodeEvent ($ node , $ this );
147
+ $ this ->dispatcher ->dispatch ('cmf_menu.create_menu_item_from_node ' , $ event );
148
+
149
+ if ($ event ->getSkipNode ()) {
150
+ return null ;
151
+ }
152
+
153
+ $ item = $ event ->getItem () ?: $ this ->createItem ($ node ->getName (), $ node ->getOptions ());
173
154
174
155
if (empty ($ item )) {
175
156
return null ;
176
157
}
177
158
178
- foreach ($ node ->getChildren () as $ childNode ) {
179
- if (!$ this ->securityContext ->isGranted ($ this ->publishWorkflowPermission , $ childNode )) {
180
- continue ;
181
- }
159
+ if ($ event ->getSkipChildren ()) {
160
+ return $ item ;
161
+ }
182
162
163
+ return $ this ->addChildrenFromNode ($ node , $ item );
164
+ }
165
+
166
+ /**
167
+ * Add children to a menu item from a node
168
+ *
169
+ * @param NodeInterface $node
170
+ * @param ItemInterface $item
171
+ * @return ItemInterface
172
+ */
173
+ public function addChildrenFromNode (NodeInterface $ node , ItemInterface $ item )
174
+ {
175
+ foreach ($ node ->getChildren () as $ childNode ) {
183
176
if ($ childNode instanceof NodeInterface) {
184
177
$ child = $ this ->createFromNode ($ childNode );
185
178
if (!empty ($ child )) {
0 commit comments