TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Localization\Event;
|
||||
|
||||
use TYPO3\CMS\Backend\Localization\LocalizationHandlerRegistry;
|
||||
use TYPO3\CMS\Backend\Localization\LocalizationInstructions;
|
||||
|
||||
/**
|
||||
* Fired in {@see LocalizationHandlerRegistry::getAvailableHandlers()} for each registered
|
||||
* handler to allow overriding `isAvailable` state returned by the handler `isAvailable()`
|
||||
* method. Main use-case is to disable handlers for special cases not implemented in the
|
||||
* handler and mitigate the need to xclass them and reduces headaches in instances and for
|
||||
* extension developers.
|
||||
*/
|
||||
final class ModifyLocalizationHandlerIsAvailableEvent
|
||||
{
|
||||
public function __construct(
|
||||
public readonly string $identifier,
|
||||
public readonly string $className,
|
||||
public readonly LocalizationInstructions $instructions,
|
||||
public bool $isAvailable,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Localization\Finisher;
|
||||
|
||||
/**
|
||||
* Interface for finishers after localization
|
||||
*
|
||||
* Finishers tell the frontend what to do after a successful localization,
|
||||
* such as redirecting to a page, loading a JavaScript module, or executing custom logic.
|
||||
*
|
||||
* @internal This API is not yet stable and may change before the LTS release
|
||||
*/
|
||||
interface LocalizationFinisherInterface extends \JsonSerializable
|
||||
{
|
||||
/**
|
||||
* Get the finisher type identifier
|
||||
*
|
||||
* This is used by the frontend to determine which handler to use
|
||||
* (e.g., 'redirect', 'noop', 'reload')
|
||||
*/
|
||||
public function getIdentifier(): string;
|
||||
|
||||
/**
|
||||
* Get the JavaScript module path for this finisher
|
||||
*
|
||||
* This module will be dynamically loaded by the frontend to handle
|
||||
* the finisher's rendering and execution logic.
|
||||
*
|
||||
* @return string The module path (e.g., '@typo3/backend/localization/finisher/redirect-finisher.js')
|
||||
*/
|
||||
public function getModule(): string;
|
||||
|
||||
/**
|
||||
* Get the finisher data as an array
|
||||
*
|
||||
* This data will be passed to the frontend handler
|
||||
*/
|
||||
public function getData(): array;
|
||||
|
||||
/**
|
||||
* Get all labels needed by this finisher's JavaScript module
|
||||
*
|
||||
* Implementations should provide pre-processed UI strings that will be passed through
|
||||
* to the frontend without any server-side resolution. The finisher implementation is
|
||||
* responsible for translating and formatting these strings.
|
||||
*
|
||||
* The array should be a simple key-value map where keys are arbitrary identifiers
|
||||
* used by the JavaScript module, and values are the ready-to-display strings.
|
||||
*
|
||||
* @return array<string, string> Label identifier => Ready-to-display string
|
||||
*/
|
||||
public function getLabels(): array;
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Localization\Finisher;
|
||||
|
||||
use TYPO3\CMS\Core\Localization\LanguageService;
|
||||
|
||||
/**
|
||||
* Finisher for when no localization operation was performed
|
||||
*
|
||||
* Used when the localization wizard completes but no actual work was done
|
||||
* (e.g., page already translated, no content selected). Offers to reload the page.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final readonly class NoopLocalizationFinisher implements LocalizationFinisherInterface
|
||||
{
|
||||
public function getIdentifier(): string
|
||||
{
|
||||
return 'noop';
|
||||
}
|
||||
|
||||
public function getModule(): string
|
||||
{
|
||||
return '@typo3/backend/wizard/finisher/noop-finisher.js';
|
||||
}
|
||||
|
||||
public function getData(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function getLabels(): array
|
||||
{
|
||||
return [
|
||||
'successTitle' => $this->getLanguageService()->sL('backend.wizards.localization:localization_wizard.finisher.noop.success.title'),
|
||||
'successDescription' => $this->getLanguageService()->sL('backend.wizards.localization:localization_wizard.finisher.noop.success.description'),
|
||||
];
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'identifier' => $this->getIdentifier(),
|
||||
'module' => $this->getModule(),
|
||||
'data' => $this->getData(),
|
||||
'labels' => $this->getLabels(),
|
||||
];
|
||||
}
|
||||
|
||||
private function getLanguageService(): LanguageService
|
||||
{
|
||||
return $GLOBALS['LANG'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Localization\Finisher;
|
||||
|
||||
use TYPO3\CMS\Core\Localization\LanguageService;
|
||||
|
||||
/**
|
||||
* Finisher to redirect to a specific URL after localization
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final readonly class RedirectLocalizationFinisher implements LocalizationFinisherInterface
|
||||
{
|
||||
public function __construct(
|
||||
public string $url,
|
||||
) {}
|
||||
|
||||
public function getIdentifier(): string
|
||||
{
|
||||
return 'redirect';
|
||||
}
|
||||
|
||||
public function getModule(): string
|
||||
{
|
||||
return '@typo3/backend/wizard/finisher/redirect-finisher.js';
|
||||
}
|
||||
|
||||
public function getData(): array
|
||||
{
|
||||
return [
|
||||
'url' => $this->url,
|
||||
];
|
||||
}
|
||||
|
||||
public function getLabels(): array
|
||||
{
|
||||
return [
|
||||
'successTitle' => $this->getLanguageService()->sL('backend.wizards.localization:localization_wizard.finisher.redirect.success.title'),
|
||||
'successDescription' => $this->getLanguageService()->sL('backend.wizards.localization:localization_wizard.finisher.redirect.success.description'),
|
||||
];
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'identifier' => $this->getIdentifier(),
|
||||
'module' => $this->getModule(),
|
||||
'data' => $this->getData(),
|
||||
'labels' => $this->getLabels(),
|
||||
];
|
||||
}
|
||||
|
||||
private function getLanguageService(): LanguageService
|
||||
{
|
||||
return $GLOBALS['LANG'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Localization\Finisher;
|
||||
|
||||
use TYPO3\CMS\Core\Localization\LanguageService;
|
||||
|
||||
/**
|
||||
* Finisher to reload the current page/content frame after localization
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final readonly class ReloadLocalizationFinisher implements LocalizationFinisherInterface
|
||||
{
|
||||
public function getIdentifier(): string
|
||||
{
|
||||
return 'reload';
|
||||
}
|
||||
|
||||
public function getModule(): string
|
||||
{
|
||||
return '@typo3/backend/wizard/finisher/reload-finisher.js';
|
||||
}
|
||||
|
||||
public function getData(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function getLabels(): array
|
||||
{
|
||||
return [
|
||||
'successTitle' => $this->getLanguageService()->sL('backend.wizards.localization:localization_wizard.finisher.reload.success.title'),
|
||||
'successDescription' => $this->getLanguageService()->sL('backend.wizards.localization:localization_wizard.finisher.reload.success.description'),
|
||||
];
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'identifier' => $this->getIdentifier(),
|
||||
'module' => $this->getModule(),
|
||||
'data' => $this->getData(),
|
||||
'labels' => $this->getLabels(),
|
||||
];
|
||||
}
|
||||
|
||||
private function getLanguageService(): LanguageService
|
||||
{
|
||||
return $GLOBALS['LANG'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Localization;
|
||||
|
||||
/**
|
||||
* Interface for localization handlers that perform the actual localization logic
|
||||
*
|
||||
* Handlers are responsible for executing the localization based on the selected mode
|
||||
*
|
||||
* @internal This API is not yet stable and may change before the LTS release
|
||||
*/
|
||||
interface LocalizationHandlerInterface
|
||||
{
|
||||
/**
|
||||
* Get a unique identifier for this handler
|
||||
*
|
||||
* @return string Unique identifier (e.g., 'manual', 'deepl', 'google_translate')
|
||||
*/
|
||||
public function getIdentifier(): string;
|
||||
|
||||
/**
|
||||
* Get a human-readable label for this handler
|
||||
*
|
||||
* @return string Label that will be displayed in the UI (can be a translation key LLL:...)
|
||||
*/
|
||||
public function getLabel(): string;
|
||||
|
||||
/**
|
||||
* Get a description of what this handler does
|
||||
*
|
||||
* @return string Description shown to help users choose the right handler (can be a translation key LLL:...)
|
||||
*/
|
||||
public function getDescription(): string;
|
||||
|
||||
/**
|
||||
* Get the icon identifier for this handler
|
||||
*
|
||||
* @return string Icon identifier that can be resolved by the IconFactory
|
||||
*/
|
||||
public function getIconIdentifier(): string;
|
||||
|
||||
/**
|
||||
* Check if this handler is available for the given localization context
|
||||
*
|
||||
* This is a pre-flight check before showing the handler to the user.
|
||||
* It receives the same context as processLocalization() to determine if
|
||||
* the handler can process this specific localization request.
|
||||
*
|
||||
* Handlers can use this to determine if they support:
|
||||
* - Specific record types (e.g., only pages or tt_content)
|
||||
* - Specific source/target language combinations
|
||||
* - Specific localization modes (copy vs translate)
|
||||
* - Other contextual requirements (e.g., API keys configured, specific records)
|
||||
*
|
||||
* @return bool True if this handler can process the localization in this context
|
||||
*/
|
||||
public function isAvailable(LocalizationInstructions $instructions): bool;
|
||||
|
||||
/**
|
||||
* Process the localization for the given records
|
||||
*
|
||||
* @return LocalizationResult The result of the localization operation
|
||||
*/
|
||||
public function processLocalization(LocalizationInstructions $instructions): LocalizationResult;
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Localization;
|
||||
|
||||
use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
use TYPO3\CMS\Backend\Localization\Event\ModifyLocalizationHandlerIsAvailableEvent;
|
||||
|
||||
/**
|
||||
* Registry for localization handlers
|
||||
*
|
||||
* Manages all available localization handlers and provides methods to
|
||||
* retrieve them by identifier or get all available handlers
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class LocalizationHandlerRegistry
|
||||
{
|
||||
/**
|
||||
* @var array<string, LocalizationHandlerInterface>
|
||||
*/
|
||||
private array $handlers = [];
|
||||
|
||||
/**
|
||||
* @param iterable<LocalizationHandlerInterface> $handlers
|
||||
*/
|
||||
public function __construct(
|
||||
iterable $handlers,
|
||||
private readonly EventDispatcherInterface $eventDispatcher,
|
||||
) {
|
||||
foreach ($handlers as $handler) {
|
||||
$this->handlers[$handler->getIdentifier()] = $handler;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a handler by its identifier
|
||||
*
|
||||
* @throws \InvalidArgumentException if handler not found
|
||||
*/
|
||||
public function getHandler(string $identifier): LocalizationHandlerInterface
|
||||
{
|
||||
if (!$this->hasHandler($identifier)) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf('Localization handler "%s" not found', $identifier),
|
||||
1733832000
|
||||
);
|
||||
}
|
||||
|
||||
return $this->handlers[$identifier];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a handler with the given identifier exists
|
||||
*/
|
||||
public function hasHandler(string $identifier): bool
|
||||
{
|
||||
return isset($this->handlers[$identifier]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available handlers for the given localization context
|
||||
*
|
||||
* Filters handlers based on their availability for the specific context.
|
||||
*
|
||||
* @return array<string, LocalizationHandlerInterface> Available handlers indexed by identifier
|
||||
*/
|
||||
public function getAvailableHandlers(LocalizationInstructions $instructions): array
|
||||
{
|
||||
$availableHandlers = [];
|
||||
foreach ($this->handlers as $handler) {
|
||||
$isAvailable = $this->eventDispatcher->dispatch(new ModifyLocalizationHandlerIsAvailableEvent(
|
||||
identifier: $handler->getIdentifier(),
|
||||
className: $handler::class,
|
||||
instructions: $instructions,
|
||||
isAvailable: $handler->isAvailable($instructions),
|
||||
))->isAvailable;
|
||||
if ($isAvailable === true) {
|
||||
$availableHandlers[$handler->getIdentifier()] = $handler;
|
||||
}
|
||||
}
|
||||
|
||||
return $availableHandlers;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Localization;
|
||||
|
||||
/**
|
||||
* A DTO for transferring localization information
|
||||
*/
|
||||
final readonly class LocalizationInstructions
|
||||
{
|
||||
public function __construct(
|
||||
// The record type (e.g., 'pages', 'tt_content')
|
||||
public string $mainRecordType,
|
||||
// The record UID to localize
|
||||
public int $recordUid,
|
||||
// The source language UID
|
||||
public int $sourceLanguageId,
|
||||
// The target language UID
|
||||
public int $targetLanguageId,
|
||||
public LocalizationMode $mode,
|
||||
// Additional data from the wizard steps (e.g., selected records for pages)
|
||||
public array $additionalData
|
||||
) {}
|
||||
|
||||
public static function create(array $parameters): self
|
||||
{
|
||||
if (!isset($parameters['recordType'], $parameters['recordUid'], $parameters['sourceLanguage'], $parameters['targetLanguage'], $parameters['mode'])) {
|
||||
throw new \InvalidArgumentException('Missing required parameters given', 1762977203);
|
||||
}
|
||||
|
||||
// Convert mode string to enum
|
||||
// We do not use tryFrom() so a valueError can be thrown
|
||||
$mode = LocalizationMode::from($parameters['mode']);
|
||||
|
||||
return new self(
|
||||
$parameters['recordType'],
|
||||
(int)$parameters['recordUid'],
|
||||
(int)$parameters['sourceLanguage'],
|
||||
(int)$parameters['targetLanguage'],
|
||||
$mode,
|
||||
$parameters['additionalData'] ?? []
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Localization;
|
||||
|
||||
/**
|
||||
* Enum representing available localization modes
|
||||
*
|
||||
* Each mode describes a different strategy for localizing records.
|
||||
*/
|
||||
enum LocalizationMode: string
|
||||
{
|
||||
case COPY = 'copy';
|
||||
case TRANSLATE = 'localize';
|
||||
|
||||
/**
|
||||
* Get the human-readable label for this mode
|
||||
*/
|
||||
public function getLabel(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::COPY => 'backend.layout:localize.wizard.button.copy',
|
||||
self::TRANSLATE => 'backend.layout:localize.wizard.button.translate',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the description for this mode
|
||||
*/
|
||||
public function getDescription(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::COPY => 'backend.layout:localize.educate.copy',
|
||||
self::TRANSLATE => 'backend.layout:localize.educate.translate',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the icon identifier for this mode
|
||||
*/
|
||||
public function getIconIdentifier(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::COPY => 'actions-edit-copy',
|
||||
self::TRANSLATE => 'actions-localize',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the priority of this mode (higher number = higher priority)
|
||||
*/
|
||||
public function getPriority(): int
|
||||
{
|
||||
return match ($this) {
|
||||
self::COPY => 10,
|
||||
self::TRANSLATE => 20,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DataHandler command for this mode
|
||||
*/
|
||||
public function getDataHandlerCommand(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::COPY => 'copyToLanguage',
|
||||
self::TRANSLATE => 'localize',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Export mode data for JSON serialization (for API responses)
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'key' => $this->value,
|
||||
'label' => $this->getLabel(),
|
||||
'description' => $this->getDescription(),
|
||||
'iconIdentifier' => $this->getIconIdentifier(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Localization;
|
||||
|
||||
use TYPO3\CMS\Backend\Localization\Finisher\LocalizationFinisherInterface;
|
||||
|
||||
/**
|
||||
* Result object for localization operations
|
||||
*
|
||||
* Contains the outcome of a localization operation and optional finisher
|
||||
* that tells the frontend what to do next (e.g., redirect, load a module, reload).
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final readonly class LocalizationResult implements \JsonSerializable
|
||||
{
|
||||
/**
|
||||
* @param bool $success Whether the localization was successful
|
||||
* @param LocalizationFinisherInterface|null $finisher Finisher to execute after localization (required for success)
|
||||
* @param array<string> $errors Array of error messages (if any)
|
||||
*/
|
||||
public function __construct(
|
||||
public bool $success = true,
|
||||
public ?LocalizationFinisherInterface $finisher = null,
|
||||
public array $errors = []
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Create a successful result
|
||||
*/
|
||||
public static function success(
|
||||
LocalizationFinisherInterface $finisher
|
||||
): self {
|
||||
return new self(
|
||||
success: true,
|
||||
finisher: $finisher
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a failed result with error messages
|
||||
*
|
||||
* @param array<string> $errors
|
||||
*/
|
||||
public static function error(array $errors): self
|
||||
{
|
||||
return new self(
|
||||
success: false,
|
||||
errors: $errors
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the result has errors
|
||||
*/
|
||||
public function hasErrors(): bool
|
||||
{
|
||||
return !empty($this->errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the result is successful
|
||||
*/
|
||||
public function isSuccess(): bool
|
||||
{
|
||||
return $this->success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert to array for JSON serialization
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
$data = [
|
||||
'success' => $this->success,
|
||||
];
|
||||
|
||||
if ($this->finisher !== null) {
|
||||
$data['finisher'] = $this->finisher->jsonSerialize();
|
||||
}
|
||||
|
||||
if (!empty($this->errors)) {
|
||||
$data['errors'] = $this->errors;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Backend\Localization;
|
||||
|
||||
use TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository;
|
||||
use TYPO3\CMS\Backend\Localization\Finisher\NoopLocalizationFinisher;
|
||||
use TYPO3\CMS\Backend\Localization\Finisher\RedirectLocalizationFinisher;
|
||||
use TYPO3\CMS\Backend\Localization\Finisher\ReloadLocalizationFinisher;
|
||||
use TYPO3\CMS\Backend\Routing\UriBuilder;
|
||||
use TYPO3\CMS\Backend\Utility\BackendUtility;
|
||||
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
|
||||
use TYPO3\CMS\Core\DataHandling\DataHandler;
|
||||
use TYPO3\CMS\Core\Localization\LanguageService;
|
||||
use TYPO3\CMS\Core\Resource\ResourceFactory;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* Manual localization handler
|
||||
*
|
||||
* Handles manual localization through the wizard interface, supporting both
|
||||
* copy and translate operations based on the selected mode
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
readonly class ManualLocalizationHandler implements LocalizationHandlerInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected UriBuilder $uriBuilder,
|
||||
protected LocalizationRepository $localizationRepository,
|
||||
protected ResourceFactory $resourceFactory,
|
||||
) {}
|
||||
|
||||
public function getIdentifier(): string
|
||||
{
|
||||
return 'manual';
|
||||
}
|
||||
|
||||
public function getLabel(): string
|
||||
{
|
||||
return 'backend.wizards.localization:handler.manual.label';
|
||||
}
|
||||
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'backend.wizards.localization:handler.manual.description';
|
||||
}
|
||||
|
||||
public function getIconIdentifier(): string
|
||||
{
|
||||
return 'actions-translate';
|
||||
}
|
||||
|
||||
public function isAvailable(LocalizationInstructions $instructions): bool
|
||||
{
|
||||
// Manual localization handler is always available
|
||||
// It supports all modes, record types, and language combinations
|
||||
return true;
|
||||
}
|
||||
|
||||
public function processLocalization(LocalizationInstructions $instructions): LocalizationResult
|
||||
{
|
||||
// Handle pages with optional content selection
|
||||
if ($instructions->mainRecordType === 'pages') {
|
||||
return $this->processPageLocalization($instructions->mode, $instructions->recordUid, $instructions->targetLanguageId, $instructions->additionalData);
|
||||
}
|
||||
|
||||
// Handle single record localization for other record types
|
||||
return $this->processSingleRecordLocalization($instructions->mode, $instructions->mainRecordType, $instructions->recordUid, $instructions->targetLanguageId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process single record localization (non-page records)
|
||||
*/
|
||||
protected function processSingleRecordLocalization(
|
||||
LocalizationMode $mode,
|
||||
string $type,
|
||||
int $uid,
|
||||
int $targetLanguage
|
||||
): LocalizationResult {
|
||||
// Validate that the record exists
|
||||
$record = BackendUtility::getRecord($type, $uid);
|
||||
if (!$record) {
|
||||
return LocalizationResult::error(
|
||||
[
|
||||
sprintf(
|
||||
$this->getLanguageService()->sL('backend.wizards.localization:error.recordNotFound'),
|
||||
$uid,
|
||||
$type
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
// Check if translation already exists
|
||||
$existingTranslation = $this->localizationRepository->getRecordTranslation($type, $uid, $targetLanguage);
|
||||
if ($existingTranslation !== null) {
|
||||
// Translation already exists, return success with no-op finisher
|
||||
return LocalizationResult::success(
|
||||
new NoopLocalizationFinisher()
|
||||
);
|
||||
}
|
||||
|
||||
$cmd = [
|
||||
$type => [
|
||||
$uid => [
|
||||
$mode->getDataHandlerCommand() => $targetLanguage,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
|
||||
$dataHandler->start([], $cmd);
|
||||
$dataHandler->process_cmdmap();
|
||||
|
||||
if ($dataHandler->errorLog !== []) {
|
||||
return LocalizationResult::error($dataHandler->errorLog);
|
||||
}
|
||||
|
||||
// Get the newly created record UID from DataHandler's copy mapping
|
||||
$newUid = $dataHandler->copyMappingArray_merged[$type][$uid] ?? null;
|
||||
|
||||
// If no UID was found in copy mapping, try to find the translated record
|
||||
if ($newUid === null) {
|
||||
$translation = $this->localizationRepository->getRecordTranslation($type, $uid, $targetLanguage);
|
||||
$newUid = $translation?->getUid();
|
||||
}
|
||||
|
||||
// Generate redirect finisher or use reload finisher as fallback
|
||||
$redirectUrl = $newUid !== null ? $this->generateRedirectUrl($type, $newUid, $targetLanguage) : null;
|
||||
|
||||
return LocalizationResult::success(
|
||||
$redirectUrl !== null
|
||||
? new RedirectLocalizationFinisher($redirectUrl)
|
||||
: new ReloadLocalizationFinisher()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process page localization including selected content elements
|
||||
*/
|
||||
protected function processPageLocalization(
|
||||
LocalizationMode $mode,
|
||||
int $pageUid,
|
||||
int $targetLanguage,
|
||||
array $additionalData
|
||||
): LocalizationResult {
|
||||
// Get selected content elements from additionalData
|
||||
$selectedContent = $additionalData['selectedRecordUids'] ?? [];
|
||||
|
||||
$cmd = [];
|
||||
|
||||
// Step 1: Check if page translation already exists
|
||||
$pageTranslation = $this->localizationRepository->getPageTranslations($pageUid, [$targetLanguage], $this->getBackendUser()->workspace);
|
||||
if ($pageTranslation === []) {
|
||||
// Page translation doesn't exist - create it
|
||||
// Always use 'localize' command for pages (even for copy mode)
|
||||
// as we need to create a proper page translation/overlay
|
||||
$cmd['pages'] = [
|
||||
$pageUid => [
|
||||
'localize' => $targetLanguage,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
// Step 2: Add selected content elements to the command
|
||||
if (!empty($selectedContent)) {
|
||||
$cmd['tt_content'] = [];
|
||||
foreach ($selectedContent as $contentUid) {
|
||||
$cmd['tt_content'][(int)$contentUid] = [
|
||||
$mode->getDataHandlerCommand() => $targetLanguage,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// If no commands were built (page already exists, no content selected),
|
||||
// still return success with a no-op finisher
|
||||
if (empty($cmd)) {
|
||||
// Use no-op finisher to indicate nothing was done, but offer to reload
|
||||
|
||||
return LocalizationResult::success(
|
||||
new NoopLocalizationFinisher()
|
||||
);
|
||||
}
|
||||
|
||||
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
|
||||
$dataHandler->start([], $cmd);
|
||||
$dataHandler->process_cmdmap();
|
||||
|
||||
if ($dataHandler->errorLog !== []) {
|
||||
return LocalizationResult::error($dataHandler->errorLog);
|
||||
}
|
||||
|
||||
// Generate redirect finisher to the page layout in the target language or use reload finisher as fallback
|
||||
$redirectUrl = $this->generateRedirectUrl('pages', $pageUid, $targetLanguage);
|
||||
return LocalizationResult::success(
|
||||
$redirectUrl !== null
|
||||
? new RedirectLocalizationFinisher($redirectUrl)
|
||||
: new ReloadLocalizationFinisher()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate redirect URL based on record type
|
||||
*/
|
||||
protected function generateRedirectUrl(string $type, int $uid, int $targetLanguage): ?string
|
||||
{
|
||||
if ($type === 'pages') {
|
||||
// Redirect to page layout module with the target language
|
||||
return (string)$this->uriBuilder->buildUriFromRoute('web_layout', [
|
||||
'id' => $uid,
|
||||
'languages' => [$targetLanguage],
|
||||
]);
|
||||
}
|
||||
|
||||
// For other record types, redirect to the edit form of the translated record
|
||||
$record = BackendUtility::getRecord($type, $uid);
|
||||
if ($record && isset($record['pid'])) {
|
||||
$returnUrl = null;
|
||||
|
||||
if ($type === 'sys_file_metadata') {
|
||||
// Get the file from the metadata record and build return URL to filelist module
|
||||
try {
|
||||
$file = $this->resourceFactory->getFileObject((int)$record['file']);
|
||||
$parentFolder = $file->getParentFolder();
|
||||
$returnUrl = (string)$this->uriBuilder->buildUriFromRoute(
|
||||
'media_management',
|
||||
['id' => $parentFolder->getCombinedIdentifier()]
|
||||
);
|
||||
} catch (\Exception) {
|
||||
// File not found or inaccessible, fall back to default return URL
|
||||
}
|
||||
}
|
||||
|
||||
if ($returnUrl === null) {
|
||||
$returnUrl = (string)$this->uriBuilder->buildUriFromRoute(
|
||||
'web_layout',
|
||||
[
|
||||
'id' => $record['pid'],
|
||||
'languages' => [$targetLanguage],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
// Redirect to edit form for the newly created record
|
||||
return (string)$this->uriBuilder->buildUriFromRoute('record_edit', [
|
||||
'edit' => [
|
||||
$type => [
|
||||
$uid => 'edit',
|
||||
],
|
||||
],
|
||||
'returnUrl' => $returnUrl,
|
||||
]);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function getLanguageService(): LanguageService
|
||||
{
|
||||
return $GLOBALS['LANG'];
|
||||
}
|
||||
|
||||
protected function getBackendUser(): BackendUserAuthentication
|
||||
{
|
||||
return $GLOBALS['BE_USER'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user