Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview draft-only translations #397

Merged
merged 3 commits into from
Jan 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -36,6 +36,28 @@ public static function getSubscribedEvents() {
}

public function onKernelRequest(RequestEvent $event): void {
$redirect = $this->getMissingDefaultRevisionRedirect($event)
?? $this->getMissingTranslationRedirect($event);
if ($redirect) {
// Add the necessary cache contexts to the response, as redirect
// responses are cached as well.
$metadata = new CacheableMetadata();
$metadata->addCacheContexts([
'languages:language_interface',
'url.query_args',
'user',
]);
$redirect->addCacheableDependency($metadata);
if ($this->routeMatch->getRouteName() === 'entity.node.canonical') {
$node = $this->routeMatch->getCurrentRouteMatch()->getParameter('node');
$redirect->addCacheableDependency($node);
}

$event->setResponse($redirect);
}
}

private function getMissingTranslationRedirect(RequestEvent $event): ?TrustedRedirectResponse {
// In case the user tries to access a node in a language entity is not
// translated to, we redirect to the entity in the original language and
// display a warning message.
@@ -57,16 +79,43 @@ public function onKernelRequest(RequestEvent $event): void {
}
$urlString = $url->toString() . '?' . $queryString . 'content_language_not_available=true&requested_language=' . $requestedLanguageId;

// Add the necessary cache contexts to the response, as redirect
// responses are cached as well.
$metadata = new CacheableMetadata();
$metadata->addCacheContexts(['languages:language_interface', 'url.query_args']);
$response = new TrustedRedirectResponse($urlString);
$response->addCacheableDependency($entity);
$response->addCacheableDependency($metadata);
return new TrustedRedirectResponse($urlString);
}
}

$event->setResponse($response);
return NULL;
}

private function getMissingDefaultRevisionRedirect(RequestEvent $event): ?TrustedRedirectResponse {
// This spaghetti code detects if user is trying to view a translation that
// is a draft only and does not have a published revision on the canonical
// route.
// Why do we do it: The content translation overview page links to
// `/{lang}/node/{nid}`, but in the case mentioned above, the canonical
// route displays the content in the original language. Which is quite
// confusing.
if ($this->routeMatch->getRouteName() === 'entity.node.canonical') {
$entity = $this->routeMatch->getCurrentRouteMatch()->getParameter('node');
$requestedLanguageId = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId();
if ($entity->language()->getId() != $requestedLanguageId) {
$storage = \Drupal::entityTypeManager()->getStorage('node');
$latestRevisionId = $storage->getLatestTranslationAffectedRevisionId($entity->id(), $requestedLanguageId);
if ($latestRevisionId) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $latestRevision */
$latestRevision = $storage->loadRevision($latestRevisionId);
$translations = $latestRevision->getTranslationLanguages();
if (array_key_exists($requestedLanguageId, $translations)) {
$latestRevision = $latestRevision->getTranslation($requestedLanguageId);
// Bingo! We found the target case. Redirect to the latest revision.
if ($latestRevision->access('view')) {
$url = $latestRevision->toUrl('latest-version')->toString();
return new TrustedRedirectResponse($url);
}
}
}
}
}
return NULL;
}

}
29 changes: 29 additions & 0 deletions tests/e2e/specs/drupal/content-editing.spec.ts
Original file line number Diff line number Diff line change
@@ -23,4 +23,33 @@ test.describe('content-editing', () => {
// access
await expect(page.locator(':text-is("More settings")')).toHaveCount(0);
});

test('preview a draft translation', async ({ page }) => {
await page.goto(cmsUrl('/en/entity/create/node/page'));
await page
.getByLabel('Title', { exact: true })
.fill('Will have a draft translation');
await page.getByRole('button', { name: /Save|Create/ }).click();
await page.getByLabel('Change to').selectOption('published');
await page.getByLabel('Headline').fill('Will have a draft translation');
await page.getByText('Save', { exact: true }).click();
await page.getByRole('link', { name: 'Translate' }).click();
const translateUrl = page.url();
await page.getByRole('link', { name: 'Add', exact: true }).click();
await page.getByLabel('Titel', { exact: true }).fill('A draft translation');
await page.getByLabel('Ändern in').selectOption('draft');
await page.getByLabel('Headline').fill('A draft translation');
await page.getByText('Speichern (diese Übersetzung)').click();

await page.goto(translateUrl);
await page
.getByRole('link', { name: 'A draft translation', exact: true })
.click();
await expect(
page
.frameLocator('iframe')
.first()
.getByRole('heading', { name: 'A draft translation', exact: true }),
).toBeVisible();
});
});
Loading