1313 */
1414
1515use 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 ;
1720use Neos \Flow \Annotations as Flow ;
1821use Neos \Flow \Mvc \Controller \ActionController ;
1922use Neos \Flow \Mvc \View \JsonView ;
20- use Neos \Neos \Controller \CreateContentContextTrait ;
2123use Neos \Neos \Domain \Model \UserPreferences ;
24+ use Neos \Neos \Domain \NodeLabel \NodeLabelGeneratorInterface ;
2225use Neos \Neos \Service \LinkingService ;
2326use Neos \Neos \Service \UserService ;
24- use Neos \Neos \Ui \ContentRepository \Service \NodeService ;
2527
2628class PreferencesController extends ActionController
2729{
28- use CreateContentContextTrait;
29-
3030 protected const FAVOURITES_PREFERENCE = 'commandBar.favourites ' ;
3131 protected const RECENT_COMMANDS_PREFERENCE = 'commandBar.recentCommands ' ;
3232 protected const RECENT_DOCUMENTS_PREFERENCE = 'commandBar.recentDocuments ' ;
@@ -36,8 +36,9 @@ class PreferencesController extends ActionController
3636 public function __construct (
3737 protected UserService $ userService ,
3838 protected EntityManagerInterface $ entityManager ,
39- protected NodeService $ nodeService ,
4039 protected LinkingService $ linkingService ,
40+ protected ContentRepositoryRegistry $ contentRepositoryRegistry ,
41+ protected NodeLabelGeneratorInterface $ nodeLabelGenerator ,
4142 ) {
4243 }
4344
@@ -47,7 +48,9 @@ public function getPreferencesAction(): void
4748 $ this ->view ->assign ('value ' , [
4849 'favouriteCommands ' => $ preferences ->get (self ::FAVOURITES_PREFERENCE ) ?? [],
4950 '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+ ),
5154 'showBranding ' => $ this ->settings ['features ' ]['showBranding ' ],
5255 ]);
5356 }
@@ -94,23 +97,28 @@ public function addRecentCommandAction(string $commandId): void
9497 /**
9598 * Updates the list of recently used documents in the user preferences
9699 *
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
99101 */
102+ #[Flow \SkipCsrfProtection]
100103 public function addRecentDocumentAction (string $ nodeContextPath ): void
101104 {
102105 $ preferences = $ this ->getUserPreferences ();
103106
107+ /** @var string[]|null $recentDocuments */
104108 $ recentDocuments = $ preferences ->get (self ::RECENT_DOCUMENTS_PREFERENCE );
105109 if ($ recentDocuments === null ) {
106110 $ recentDocuments = [];
107111 }
108112
109113 // 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+
112119 // Add the path to the top of the list
113120 array_unshift ($ recentDocuments , $ nodeContextPath );
121+
114122 // Limit the list to 5 items
115123 $ recentDocuments = array_slice ($ recentDocuments , 0 , 5 );
116124
@@ -130,32 +138,52 @@ protected function getUserPreferences(): UserPreferences
130138 }
131139
132140 /**
133- * @var string[] $contextPaths
141+ * @param string[] $nodeContextPaths
134142 */
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+
140166 $ 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 ;
148169 }
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+ );
152180 }
153181
154- protected function getNodeUri (NodeInterface $ node ): string
182+ protected function getNodeUri (Node $ node ): string
155183 {
156184 try {
157185 return $ this ->linkingService ->createNodeUri ($ this ->controllerContext , $ node , null , 'html ' , true );
158- } catch (\Exception $ e ) {
186+ } catch (\Exception ) {
159187 return '' ;
160188 }
161189 }
0 commit comments