Created
September 21, 2018 06:59
-
-
Save froemken/a8a2529c5a1ab475074e1ad26ec76e57 to your computer and use it in GitHub Desktop.
Show translated labels in selectbox of relations in backend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Elo\EloCareer\Tca; | |
/* | |
* 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! | |
*/ | |
use TYPO3\CMS\Backend\Form\FormDataProvider\TcaSelectItems; | |
use TYPO3\CMS\Backend\Utility\BackendUtility; | |
use TYPO3\CMS\Core\Database\ConnectionPool; | |
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
/** | |
* Class TcaSelectItemsProcessor | |
*/ | |
class TcaSelectItemsProcessor | |
{ | |
/** | |
* @param array $parentArray | |
* @param TcaSelectItems $parentObj | |
*/ | |
public function translateTable1Selector($parentArray, TcaSelectItems $parentObj) | |
{ | |
$parentArray['items'] = $this->translateItemsForTable( | |
$parentArray['items'], | |
'tx_mytable_domain_model_table1', | |
'type' | |
); | |
} | |
/** | |
* @param array $items | |
* @param string $table | |
* @param string $fieldName | |
* @return array | |
*/ | |
protected function translateItemsForTable($items, $table, $fieldName) | |
{ | |
if (is_array($items)) { | |
foreach ($items as $key => $item) { | |
$translatedRecord = BackendUtility::getRecordLocalization( | |
$table, | |
(int)$item[1], | |
$this->getLanguageUidFromBeUser() | |
); | |
if (!empty($translatedRecord)) { | |
$items[$key][0] = $translatedRecord[0][$fieldName]; | |
} | |
} | |
} | |
return $items; | |
} | |
/** | |
* @return int | |
*/ | |
protected function getLanguageUidFromBeUser() | |
{ | |
$userConfiguration = $this->getBackendUserAuthentication()->uc; | |
$iso2 = $userConfiguration['lang'] ?: 'en'; | |
$queryBuilder = $this->getConnectionPool()->getQueryBuilderForTable('sys_language'); | |
$queryBuilder->getRestrictions()->removeAll(); | |
$queryBuilder->getRestrictions()->add( | |
GeneralUtility::makeInstance(DeletedRestriction::class) | |
); | |
$row = $queryBuilder | |
->select('uid') | |
->from('sys_language') | |
->where( | |
$queryBuilder->expr()->eq( | |
'language_isocode', | |
$queryBuilder->createNamedParameter($iso2, \PDO::PARAM_STR) | |
) | |
) | |
->execute() | |
->fetch(); | |
if (empty($row)) { | |
return 0; // default language (german) | |
} | |
return (int)$row['uid']; | |
} | |
/** | |
* @return mixed|\TYPO3\CMS\Core\Authentication\BackendUserAuthentication | |
*/ | |
protected function getBackendUserAuthentication() | |
{ | |
return $GLOBALS['BE_USER']; | |
} | |
/** | |
* Get TYPO3s Connection Pool | |
* | |
* @return ConnectionPool | |
*/ | |
protected function getConnectionPool() | |
{ | |
return GeneralUtility::makeInstance(ConnectionPool::class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment