Skip to content

Commit f76366c

Browse files
Merge pull request #66 from sitegeist/bugfix-translate-on-shutdown
BUGFIX: Snyc nodes on shutdown
2 parents 6963153 + 3fe40d6 commit f76366c

File tree

2 files changed

+62
-11
lines changed

2 files changed

+62
-11
lines changed

Classes/ContentRepository/NodeTranslationService.php

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
use Neos\ContentRepository\Domain\Model\NodeInterface;
88
use Neos\ContentRepository\Domain\Model\Workspace;
99
use Neos\ContentRepository\Domain\Service\Context;
10-
use Neos\ContentRepository\Domain\Service\ContextFactory;
1110
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;
1211
use Neos\Flow\Annotations as Flow;
12+
use Neos\Flow\Persistence\Doctrine\PersistenceManager;
13+
use Neos\Flow\Persistence\Exception\IllegalObjectTypeException;
1314
use Neos\Neos\Service\PublishingService;
1415
use Neos\Neos\Utility\NodeUriPathSegmentGenerator;
1516
use 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
/**

Classes/Package.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Neos\Flow\Core\Bootstrap;
99
use Neos\Flow\Package\Package as BasePackage;
1010
use Neos\ContentRepository\Domain\Service\Context;
11+
use Neos\Flow\Persistence\Doctrine\PersistenceManager;
1112
use Neos\Neos\Fusion\Cache\ContentCacheFlusher;
1213
use Sitegeist\LostInTranslation\ContentRepository\NodeTranslationService;
1314

@@ -24,6 +25,7 @@ public function boot(Bootstrap $bootstrap)
2425
{
2526
$dispatcher = $bootstrap->getSignalSlotDispatcher();
2627
$dispatcher->connect(Context::class, 'afterAdoptNode', NodeTranslationService::class, 'afterAdoptNode', false);
27-
$dispatcher->connect(Workspace::class, 'afterNodePublishing', NodeTranslationService::class, 'afterNodePublish', false);
28+
$dispatcher->connect(Workspace::class, 'beforeNodePublishing', NodeTranslationService::class, 'collectNodesToBeTranslated', false);
29+
$dispatcher->connect(PersistenceManager::class, 'allObjectsPersisted', NodeTranslationService::class, 'translateNodes', false);
2830
}
2931
}

0 commit comments

Comments
 (0)