Search code examples
typo3extbasetypo3-8.xtypo3-extensionstypo3-8.7.x

Getting translated records in CommandController


I've searched and debugged the last couple of days how to obtain the translated version of a DomainModel object in a CommandController in Typo3 v8.7.

In Typo3 4.5/4.7 I've done the following: - input: DomainModel in default language - build a query that finds the record with the l10n_parent matching the given domain model - obtain a new domain model with the desired sys_language_uid

Unfortunately this does not work in Typo3 v8.7 any more. I always get the domain model for the default language.

I've traced this down to the method Typo3DbBackend::doLanguageAndWorkspaceOverlay called via Typo3DbBackend::getObjectDataByQuery

The query returns the correct (translated) row (seen in the debugger and also the mysql query log), but then the variable $row gets overwritten in doLanguageAndWorkspaceOverlay no matter how I set the querySettings setLanguageOverlayMode and setLanguageMode.

So what is the correct way to get a translated domain model in a CommandController?

UPDATE:

I think I'm a step further. If I add ->setQueryLanguage(1) to the query settings, doLanguageAndWorkspaceOverlay() tries to fetch the translated record for language = 1. But in order to succeed I need to trick the FrontendGroupRestriction class by setting $GLOBALS['TSFE']->gr_list = "0,-2";.

The array returned by doLanguageAndWorkspaceOverlay() now contains all translated entries, except the uid, which is still the uid from the record in the main language. The uid of the translated record is stored in _LOCALIZED_UID.

Now my problem now is that I still get the record in the main laguage because DataMapper->mapSingleRow() (called via DataMapper->map()) has some kind of object cache and thus returns the object in the default language (because the uid is still the one of the record in the main language).

All this seems a little hackish. So again my question: what is the correct way to get a translated domain model in a CommandController?

thanks, mika

p.s.: I've set up the second language in the backend and creating a translated record works just fine. My question is just how to I get an existing translated record in a CommandController.


Solution

  • alternative solution:

    based on the solution above, I decided that I can do almost everything on my own. So what I do now is

    i) create an independend querybuilder for the according table:

    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);`
    

    ii) select the record with the desired l10n_parent and sys_language_uid

    $query = $queryBuilder->select('*')
             ->from($tableName)
             ->where($queryBuilder->expr()->eq('sys_language_uid', $langId))
             ->andWhere($queryBuilder->expr()->eq('l10n_parent', $parentUid))
             ->execute();
    

    iii) fetch all records into an array

    $rows = $query->fetchAll();
    

    iv) invoke the DataMapper manually to get the object

    $dataMapper = $this->objectManager->get(DataMapper::class);
    $translated = $dataMapper->map($className, $rows);
    

    I know it has nothing to do with the ModelRepository any more, but it works quite fine for now...

    that's all folks