Search code examples
typo3typo3-9.xdatahandler

TYPO3 DataHandler: PHP-programmatically get some ContentElement's translated ContentElement UID


I'm using TYPO3 9 LTS's CommandController infrastructure for preparing a "batch script" which will update a bunch of pages and content elements in my TYPO3 installation. For these updates I'm levaraging TYPO3's DataHandler.

My TYPO3 installation has German (canonical) and English localizations for pages and content elements, and these pages and content elements are in connected mode, i.e., an English content element is connected to its "original" German content element.

I know the UIDs of the German and English content elements I want to update, but to keep things concise in my script, I thought it should be enough to just reference the "canonical" German content element UIDs, and programmatically derive their English localization counterpart UIDs.

Is there a TYPO3 PHP API for getting translated content element's UID by providing the original content element UID?

E.g., I'm looking for something like

$languageIdDe = 0; // in my installation/site, German language has languageId: '0'
$languageIdEn = 1; // in my installation/site, English language has languageId: '1'

$originalContentElementUidDe = 1234;

$translatedContentElementUidEn = ContentElementTranslationUtil::getTranslatedContentElementUid($originalContentElementUidDe, $languageIdEn);

Solution

  • Yes, it is:

    \TYPO3\CMS\Backend\Utility\BackendUtility::getRecordLocalization($table, $uid, $language, $andWhereClause = '');
    

    This function returns a "mixed Multidimensional array with selected records; if none exist, FALSE is returned".

    So to get the translated content element's UID as per the question:

    use TYPO3\CMS\Backend\Utility\BackendUtility;
    
    $languageIdDe = 0; // German language has languageId: '0'
    $languageIdEn = 1; // English language has languageId: '1'
    
    $originalContentElementUidDe = 1234;
    
    $translatedContentElementUidEn = BackendUtility::getRecordLocalization(
      'tt_content',
      $originalContentElementUidDe,
      $languageIdEn
    )[0]['uid'];
    

    You can also get faster answers to questions like this in TYPO3 Slack.