77use Neos \ContentRepository \Domain \Model \NodeInterface ;
88use Neos \ContentRepository \Domain \Model \Workspace ;
99use Neos \ContentRepository \Domain \Service \Context ;
10- use Neos \ContentRepository \Domain \Service \ContextFactory ;
1110use Neos \ContentRepository \Domain \Service \ContextFactoryInterface ;
1211use Neos \Flow \Annotations as Flow ;
12+ use Neos \Flow \Persistence \Doctrine \PersistenceManager ;
13+ use Neos \Flow \Persistence \Exception \IllegalObjectTypeException ;
1314use Neos \Neos \Service \PublishingService ;
1415use Neos \Neos \Utility \NodeUriPathSegmentGenerator ;
1516use Sitegeist \LostInTranslation \Domain \TranslatableProperty \TranslatablePropertyNamesFactory ;
@@ -109,6 +110,12 @@ class NodeTranslationService
109110 */
110111 protected $ liveWorkspaceName = 'live ' ;
111112
113+ /**
114+ * @Flow\Inject
115+ * @var PersistenceManager
116+ */
117+ protected $ persistenceManager ;
118+
112119 /**
113120 * If nodes are moved in Neos, then it will move node variants in other dimensions
114121 * as well. As we already move nodes when we sync, we don't want this behaviour
@@ -118,6 +125,11 @@ class NodeTranslationService
118125 */
119126 protected bool $ recursionPreventionEnabled = true ;
120127
128+ /**
129+ * @var array<string, array<string, array<string, NodeInterface>>>
130+ */
131+ protected array $ nodesToBeTranslated = [];
132+
121133 /**
122134 * @param NodeInterface $node
123135 * @param Context $context
@@ -162,11 +174,11 @@ public function afterAdoptNode(NodeInterface $node, Context $context, bool $recu
162174 }
163175
164176 /**
165- * @param NodeInterface $node
166- * @param Workspace $workspace
177+ * @param NodeInterface $node
178+ * @param Workspace $workspace
167179 * @return void
168180 */
169- public function afterNodePublish (NodeInterface $ node , Workspace $ workspace ): void
181+ public function collectNodesToBeTranslated (NodeInterface $ node , Workspace $ workspace ): void
170182 {
171183 if (!$ this ->enabled ) {
172184 return ;
@@ -184,13 +196,50 @@ public function afterNodePublish(NodeInterface $node, Workspace $workspace): voi
184196 }
185197 }
186198
187- if ($ this ->skipAuthorizationChecks ) {
188- $ this ->securityContext ->withoutAuthorizationChecks (function () use ($ node ) {
189- $ this ->syncNode ($ node , $ this ->liveWorkspaceName );
190- });
191- } else {
192- $ this ->syncNode ($ node , $ this ->liveWorkspaceName );
199+ $ isAutomaticTranslationEnabledForNodeType = $ node ->getNodeType ()->getConfiguration ('options.automaticTranslation ' ) ?? true ;
200+ if (!$ isAutomaticTranslationEnabledForNodeType ) {
201+ return ;
193202 }
203+
204+ $ nodeSourceDimensionValue = $ node ->getContext ()->getTargetDimensions ()[$ this ->languageDimensionName ];
205+ $ defaultPreset = $ this ->contentDimensionConfiguration [$ this ->languageDimensionName ]['defaultPreset ' ];
206+
207+ if ($ nodeSourceDimensionValue !== $ defaultPreset ) {
208+ return ;
209+ }
210+
211+ $ this ->nodesToBeTranslated [$ workspace ->getName ()][$ nodeSourceDimensionValue ][$ node ->getIdentifier ()] = $ node ;
212+ }
213+
214+ /**
215+ * @return void
216+ * @throws IllegalObjectTypeException|\Exception
217+ */
218+ public function translateNodes (): void
219+ {
220+ /**
221+ * @var string $workspaceName
222+ * @var array<string, array<string, NodeInterface>> $nodesByWorkspace
223+ */
224+ foreach ($ this ->nodesToBeTranslated as $ workspaceName => $ nodesByWorkspace ) {
225+ /**
226+ * @var array<string, NodeInterface> $nodesByLanguageDimensionValue
227+ */
228+ foreach ($ nodesByWorkspace as $ nodesByLanguageDimensionValue ) {
229+ foreach ($ nodesByLanguageDimensionValue as $ node ) {
230+ if ($ this ->skipAuthorizationChecks ) {
231+ $ this ->securityContext ->withoutAuthorizationChecks (function () use ($ node , $ workspaceName ) {
232+ $ this ->syncNode ($ node , $ workspaceName );
233+ });
234+ } else {
235+ $ this ->syncNode ($ node , $ workspaceName );
236+ }
237+ }
238+ }
239+ }
240+
241+ $ this ->nodesToBeTranslated = [];
242+ $ this ->persistenceManager ->persistAll ();
194243 }
195244
196245 /**
0 commit comments