13
13
*/
14
14
15
15
use Doctrine \ORM \EntityManagerInterface ;
16
- use Neos \ContentRepository \Domain \Model \NodeInterface ;
16
+ use Neos \ContentRepository \Core \Feature \Security \Exception \AccessDenied ;
17
+ use Neos \ContentRepository \Core \Projection \ContentGraph \Node ;
18
+ use Neos \ContentRepository \Core \SharedModel \Node \NodeAddress ;
19
+ use Neos \ContentRepositoryRegistry \ContentRepositoryRegistry ;
17
20
use Neos \Flow \Annotations as Flow ;
18
21
use Neos \Flow \Mvc \Controller \ActionController ;
19
22
use Neos \Flow \Mvc \View \JsonView ;
20
- use Neos \Neos \Controller \CreateContentContextTrait ;
21
23
use Neos \Neos \Domain \Model \UserPreferences ;
24
+ use Neos \Neos \Domain \NodeLabel \NodeLabelGeneratorInterface ;
22
25
use Neos \Neos \Service \LinkingService ;
23
26
use Neos \Neos \Service \UserService ;
24
- use Neos \Neos \Ui \ContentRepository \Service \NodeService ;
25
27
26
28
class PreferencesController extends ActionController
27
29
{
28
- use CreateContentContextTrait;
29
-
30
30
protected const FAVOURITES_PREFERENCE = 'commandBar.favourites ' ;
31
31
protected const RECENT_COMMANDS_PREFERENCE = 'commandBar.recentCommands ' ;
32
32
protected const RECENT_DOCUMENTS_PREFERENCE = 'commandBar.recentDocuments ' ;
@@ -36,8 +36,9 @@ class PreferencesController extends ActionController
36
36
public function __construct (
37
37
protected UserService $ userService ,
38
38
protected EntityManagerInterface $ entityManager ,
39
- protected NodeService $ nodeService ,
40
39
protected LinkingService $ linkingService ,
40
+ protected ContentRepositoryRegistry $ contentRepositoryRegistry ,
41
+ protected NodeLabelGeneratorInterface $ nodeLabelGenerator ,
41
42
) {
42
43
}
43
44
@@ -47,7 +48,9 @@ public function getPreferencesAction(): void
47
48
$ this ->view ->assign ('value ' , [
48
49
'favouriteCommands ' => $ preferences ->get (self ::FAVOURITES_PREFERENCE ) ?? [],
49
50
'recentCommands ' => $ preferences ->get (self ::RECENT_COMMANDS_PREFERENCE ) ?? [],
50
- 'recentDocuments ' => $ this ->mapContextPathsToNodes ($ preferences ->get (self ::RECENT_DOCUMENTS_PREFERENCE ) ?? []),
51
+ 'recentDocuments ' => $ this ->mapContextPathsToNodes (
52
+ $ preferences ->get (self ::RECENT_DOCUMENTS_PREFERENCE ) ?? []
53
+ ),
51
54
'showBranding ' => $ this ->settings ['features ' ]['showBranding ' ],
52
55
]);
53
56
}
@@ -94,23 +97,28 @@ public function addRecentCommandAction(string $commandId): void
94
97
/**
95
98
* Updates the list of recently used documents in the user preferences
96
99
*
97
- * @Flow\SkipCsrfProtection
98
- * @param string $nodeContextPath a context path to add to the recently visited documents
100
+ * @param string $nodeContextPath a node to add to the recently visited documents
99
101
*/
102
+ #[Flow \SkipCsrfProtection]
100
103
public function addRecentDocumentAction (string $ nodeContextPath ): void
101
104
{
102
105
$ preferences = $ this ->getUserPreferences ();
103
106
107
+ /** @var string[]|null $recentDocuments */
104
108
$ recentDocuments = $ preferences ->get (self ::RECENT_DOCUMENTS_PREFERENCE );
105
109
if ($ recentDocuments === null ) {
106
110
$ recentDocuments = [];
107
111
}
108
112
109
113
// Remove the command from the list if it is already in there (to move it to the top)
110
- $ recentDocuments = array_filter ($ recentDocuments ,
111
- static fn ($ existingContextPath ) => $ existingContextPath !== $ nodeContextPath );
114
+ $ recentDocuments = array_filter (
115
+ $ recentDocuments ,
116
+ static fn ($ existingContextPath ) => $ existingContextPath !== $ nodeContextPath
117
+ );
118
+
112
119
// Add the path to the top of the list
113
120
array_unshift ($ recentDocuments , $ nodeContextPath );
121
+
114
122
// Limit the list to 5 items
115
123
$ recentDocuments = array_slice ($ recentDocuments , 0 , 5 );
116
124
@@ -130,32 +138,52 @@ protected function getUserPreferences(): UserPreferences
130
138
}
131
139
132
140
/**
133
- * @var string[] $contextPaths
141
+ * @param string[] $nodeContextPaths
134
142
*/
135
- protected function mapContextPathsToNodes (array $ contextPaths ): array
136
- {
137
- return array_reduce ($ contextPaths , function (array $ carry , string $ contextPath ) {
138
- $ node = $ this ->nodeService ->getNodeFromContextPath ($ contextPath );
139
- if ($ node instanceof NodeInterface) {
143
+ protected function mapContextPathsToNodes (
144
+ array $ nodeContextPaths ,
145
+ ): array {
146
+ return array_filter (
147
+ array_map (function (string $ nodeContextPath ) {
148
+ $ nodeAddress = NodeAddress::fromJsonString ($ nodeContextPath );
149
+
150
+ $ contentRepository = $ this ->contentRepositoryRegistry ->get ($ nodeAddress ->contentRepositoryId );
151
+ try {
152
+ $ subgraph = $ contentRepository ->getContentSubgraph (
153
+ $ nodeAddress ->workspaceName ,
154
+ $ nodeAddress ->dimensionSpacePoint
155
+ );
156
+ } catch (AccessDenied ) {
157
+ // If the user does not have access to the subgraph, we skip this node
158
+ return null ;
159
+ }
160
+
161
+ $ node = $ subgraph ->findNodeById ($ nodeAddress ->aggregateId );
162
+ if (!$ node ) {
163
+ return null ;
164
+ }
165
+
140
166
$ uri = $ this ->getNodeUri ($ node );
141
- if ($ uri ) {
142
- $ carry []= [
143
- 'name ' => $ node ->getLabel (),
144
- 'icon ' => $ node ->getNodeType ()->getConfiguration ('ui.icon ' ) ?? 'question ' ,
145
- 'uri ' => $ this ->getNodeUri ($ node ),
146
- 'contextPath ' => $ contextPath ,
147
- ];
167
+ if (!$ uri ) {
168
+ return null ;
148
169
}
149
- }
150
- return $ carry ;
151
- }, []);
170
+
171
+ $ nodeType = $ contentRepository ->getNodeTypeManager ()->getNodeType ($ node ->nodeTypeName );
172
+ return [
173
+ 'name ' => $ this ->nodeLabelGenerator ->getLabel ($ node ),
174
+ 'icon ' => $ nodeType ?->getConfiguration('ui.icon ' ) ?? 'question ' ,
175
+ 'uri ' => $ this ->getNodeUri ($ node ),
176
+ 'contextPath ' => $ nodeContextPath ,
177
+ ];
178
+ }, $ nodeContextPaths )
179
+ );
152
180
}
153
181
154
- protected function getNodeUri (NodeInterface $ node ): string
182
+ protected function getNodeUri (Node $ node ): string
155
183
{
156
184
try {
157
185
return $ this ->linkingService ->createNodeUri ($ this ->controllerContext , $ node , null , 'html ' , true );
158
- } catch (\Exception $ e ) {
186
+ } catch (\Exception ) {
159
187
return '' ;
160
188
}
161
189
}
0 commit comments