Skip to content

Instantly share code, notes, and snippets.

@htuscher
Last active August 29, 2023 11:04
Show Gist options
  • Save htuscher/a374f7526440ba987c19 to your computer and use it in GitHub Desktop.
Save htuscher/a374f7526440ba987c19 to your computer and use it in GitHub Desktop.
TYPO3 Extbase get record with language different than FE or 0
<?php
/** @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher */
$signalSlotDispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher');
$signalSlotDispatcher->connect(
'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend',
'beforeGettingObjectData',
'Onedrop\\Common\\Service\\ExtbaseForceLanguage',
'forceLanguageForQueries'
);
<?php
namespace Onedrop\Common\Service;
/***************************************************************
* Copyright notice
*
* (c) 2015 Hans Höchtl <[email protected]>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the textfile GPL.txt and important notices to the license
* from the author is found in LICENSE.txt distributed with these scripts.
*
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Persistence\Generic\Session;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
class ExtbaseForceLanguage implements SingletonInterface
{
protected $overrideLanguage = FALSE;
protected $languageUid = 0;
protected $languageMode = NULL;
protected $languageOverlayMode = FALSE;
/**
* This method is registered on the Signal beforeGettingObjectData of the Extbase Generic Backend
* to override the returned model language
*
* @param QueryInterface $query
* @return QueryInterface
*/
public function forceLanguageForQueries(QueryInterface $query) {
if (!$this->overrideLanguage) {
return [$query];
}
$querySettings = $query->getQuerySettings();
$querySettings->setLanguageUid($this->languageUid);
$querySettings->setLanguageMode($this->languageMode);
$querySettings->setLanguageOverlayMode($this->languageOverlayMode);
$query->setQuerySettings($querySettings);
return [$query];
}
/**
* @return boolean
*/
public function isOverrideLanguage()
{
return $this->overrideLanguage;
}
/**
* When changing the override language, we clear the persistence session of extbase,
* otherwise the non translated objects would be returned (cache).
*
* @param boolean $overrideLanguage
* @return void
*/
public function setOverrideLanguage($overrideLanguage)
{
$this->overrideLanguage = $overrideLanguage;
/** @var ObjectManager $objectManager */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/** @var Session $persistenceSession */
$persistenceSession = $objectManager->get(Session::class);
$persistenceSession->destroy();
}
/**
* @return int
*/
public function getLanguageUid()
{
return $this->languageUid;
}
/**
* @param int $languageUid
* @return void
*/
public function setLanguageUid($languageUid)
{
$this->languageUid = $languageUid;
}
/**
* @return mixed
*/
public function getLanguageMode()
{
return $this->languageMode;
}
/**
* @param mixed $languageMode
* @return void
*/
public function setLanguageMode($languageMode)
{
$this->languageMode = $languageMode;
}
/**
* @return mixed
*/
public function getLanguageOverlayMode()
{
return $this->languageOverlayMode;
}
/**
* @param mixed $languageOverlayMode
* @return void
*/
public function setLanguageOverlayMode($languageOverlayMode)
{
$this->languageOverlayMode = $languageOverlayMode;
}
}
<?php
namespace Onedrop\Common\Controller;
/***************************************************************
* Copyright notice
*
* (c) 2015 Hans Höchtl <[email protected]>, Onedrop Solutions
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the textfile GPL.txt and important notices to the license
* from the author is found in LICENSE.txt distributed with these scripts.
*
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
/**
* Class SomeController
*
* @package Onedrop\Common\Controller
*/
class SomeController extends ActionController
{
/**
* @var \Onedrop\Common\Domain\Repository\SomeRepository
* @inject
*/
protected $someRepository;
/**
* @var \Onedrop\Common\Service\ExtbaseForceLanguage
* @inject
*/
protected $extbaseForceLanguageService;
public function someAction($langUid = 0) {
$this->extbaseForceLanguageService->setOverrideLanguage(true);
$this->extbaseForceLanguageService->setLanguageUid($langUid);
$extbaseRecordWithOverlayDifferentThanFeLanguage = $this->someRepository->findByUid(1);
$this->extbaseForceLanguageService->setOverrideLanguage(false);
}
}
@arnekolja
Copy link

I had to add a small, really really dirty hack, to make it work in an importer that is started by a command controller:

if (!isset($GLOBALS['TSFE'])) {
    $GLOBALS['TSFE'] = (object) ['gr_list' => ''];
}

@joka123
Copy link

joka123 commented Jun 11, 2018

So great!!
you saved my day! Thx a lot!

@izzycole
Copy link

Even in 2021 this saved me a lot of time, thank you!

@Grony
Copy link

Grony commented Aug 16, 2022

Does it works with nested objects? Are they also retrieved in forced language of parent object (lazy loading with ObjectStorage)?
I have used your solution directly in a repository function but the nested objects are still in standard language:

public function findByUidAndLanguage($uid, $languageUid=0)
{
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/** @var Session $persistenceSession */
$persistenceSession = $objectManager->get(Session::class);
$persistenceSession->destroy();
$query = $this->persistenceManager->createQueryForType(Description::class);
$query->getQuerySettings()->setRespectSysLanguage(false);
$query->getQuerySettings()->setRespectStoragePage(false);
$query->getQuerySettings()->setLanguageUid($languageUid);
$query->getQuerySettings()->setLanguageOverlayMode(false);
$query->getQuerySettings()->setLanguageMode('content_fallback');
$query->matching($query->equals('uid', $uid));
$object = $query->execute()->getFirst();
$test = $object->getNestedObject(); // <-- They are not in language of object !! :-(
}

@Grony
Copy link

Grony commented Aug 17, 2022

I have found the mistake myself. Al sub/nested objects will be retrieved forced in language which are loaded between setOverrideLanguage(true) and setOverrideLanguage(false). My single function does not work!

@bytesinmotion
Copy link

Thanks for the code. It works great on a single showbyUid but I can't get it working on a listview. I have mixed language records and in the list query it simply does not work :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment