Fetch the translations of a database record
New in version 14.2 Feature: #108799 - LocalizationRepository methods for fetching record translations
\TYPO3\
reads the translations that exist for a record. Inject it into the class that
needs them:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Service;
use TYPO3\CMS\Backend\Domain\Repository\Localization\LocalizationRepository;
use TYPO3\CMS\Core\Domain\RawRecord;
final readonly class RecordTranslationService
{
public function __construct(
private LocalizationRepository $localizationRepository,
) {}
/**
* @return RawRecord[] indexed by language ID
*/
public function findTranslations(int $conferenceUid): array
{
return $this->localizationRepository->getRecordTranslations(
'tx_myextension_conference',
$conferenceUid,
);
}
public function findTranslation(
int $conferenceUid,
int $languageId,
): ?RawRecord {
return $this->localizationRepository->getRecordTranslation(
'tx_myextension_conference',
$conferenceUid,
$languageId,
);
}
public function countTranslations(int $conferenceUid): int
{
return count($this->findTranslations($conferenceUid));
}
}
The repository returns the translations only, never the record in the
default language. The values come as
Raw objects, which carry the
values as they are stored, without the resolved relations of a
record object.
A table that TCA does not describe, or that is not language aware, yields
null or an empty array rather than an error.
The repository applies the workspace overlay and leaves out the placeholder records of a deleted translation. It does not read the backend user, so it answers the same in a console command as in a backend module.
Methods of the localization repository
getRecord Translation ($table Or Schema, $record Or Uid, $language) - The translation of a record in one language, or
nullwhen there is none. getRecord Translations ($table Or Schema, $record Or Uid, $limit To Language Ids) - Every translation of a record, indexed by language ID.
counton the result is the number of translations.() getPage Translations ($page Uid, $limit To Language Ids) - Every translation of a page, indexed by language ID.
Name the table either as a string or as a
Tca, and the record either as its
uid, as the record array or as a
Record. Passing the whole record
lets the repository filter by the page of the record as well. The language is
a language ID or a
Language. Two further optional
arguments of every method choose the workspace and whether deleted
translations are included.