Instantly share code, notes, and snippets.
Last active
October 18, 2018 03:10
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(1)
1
You must be signed in to fork a gist
-
Save mhuber84/dce2b09f5ef17ec596bdd938f56811ff to your computer and use it in GitHub Desktop.
Set USERDEF1 (or USERDEF2) in a language menu on record detail pages if the record is not translated. #TYPO3
This file contains 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 BGM\BgmTheme\Utility; | |
/* | |
* 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\Core\Utility\GeneralUtility; | |
/** | |
* Class MenuUtility | |
*/ | |
class MenuUtility { | |
/** | |
* Check if we are on the detail view of a record and if the record is translated. | |
* If the record is not translated, the ITEM_STATE of the language menu item is set to USERDEF1 or USERDEF2 | |
* as it is also done for pages (see AbstractMenuContentObject::prepareMenuItemsForLanguageMenu() "Checking if the | |
* "disabled" state should be set."). | |
* | |
* TypoScript configuration example for EXT:news | |
* | |
* lib.languageSelector = HMENU | |
* lib.languageSelector { | |
* special = language | |
* [...] | |
* 1 = TMENU | |
* 1 { | |
* [...] | |
* itemArrayProcFunc = BGM\BgmTheme\Utility\MenuUtility->checkForRecordTranslations | |
* itemArrayProcFunc.getParameters { | |
* # GET.PARAMETER.WITH.RECORD.UID = TABLENAME | |
* tx_news_pi1.news = tx_news_domain_model_news | |
* } | |
* } | |
* } | |
* | |
* @see \TYPO3\CMS\Frontend\ContentObject\Menu\AbstractMenuContentObject::prepareMenuItemsForLanguageMenu() | |
* @param array $menuArr | |
* @param array $conf | |
* @return array $menuArr | |
*/ | |
public function checkForRecordTranslations($menuArr, $conf){ | |
$check = $this->shouldCheck($conf['getParameters.'], GeneralUtility::_GET()); | |
if($check !== false){ | |
/** @var \TYPO3\CMS\Frontend\ContentObject\Menu\TextMenuContentObject $parentObject */ | |
$parentObject = $conf['parentObj']; | |
/** @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $tsfe */ | |
$tsfe = $this->getTypoScriptFrontendController(); | |
/** @var \TYPO3\CMS\Core\Database\DatabaseConnection $db */ | |
$db = $this->getDatabaseConnection(); | |
foreach($menuArr as &$menuItem){ | |
/** @var int $sUid current menu item language id */ | |
$sUid = intval($menuItem['_PAGES_OVERLAY_LANGUAGE']); | |
// Find overlay record: | |
if ($sUid) { | |
$currentRecord = $db->exec_SELECTgetSingleRow( | |
'*', | |
$db->quoteStr($check['table'], $check['table']), | |
'uid = ' . intval($check['recordUid']) | |
); | |
$overlayMode = $GLOBALS['TSFE']->sys_language_mode === 'strict' ? 'hideNonTranslated' : ''; | |
$lRecs = $parentObject->sys_page->getRecordOverlay($check['table'], $currentRecord, $sUid, $overlayMode); | |
} else { | |
$lRecs = []; | |
} | |
// Checking if the "disabled" state should be set. | |
if (GeneralUtility::hideIfNotTranslated($tsfe->page['l18n_cfg']) && $sUid && | |
empty($lRecs) || GeneralUtility::hideIfDefaultLanguage($tsfe->page['l18n_cfg']) && | |
(!$sUid || empty($lRecs)) || | |
!$parentObject->conf['special.']['normalWhenNoLanguage'] && $sUid && empty($lRecs) | |
) { | |
$menuItem['ITEM_STATE'] = $tsfe->sys_language_uid == $sUid ? 'USERDEF2' : 'USERDEF1'; | |
} | |
} | |
} | |
return $menuArr; | |
} | |
/** | |
* Walk through GET-Parameters and TypoScript-Configuration to check if we should do something. | |
* If we should do something this function returns the tablename and uid of the record to check. | |
* Else it returns false. | |
* | |
* @param array $parameter | |
* @return mixed | |
*/ | |
protected function shouldCheck($configuration, $getParameters){ | |
$result = false; | |
foreach($configuration as $key => $configuratedParameter) { | |
$getParameterKey = $key; | |
if(is_array($configuratedParameter)){ | |
$getParameterKey = rtrim($key, '.'); | |
} | |
if (isset($getParameters[$getParameterKey])){ | |
if(is_array($getParameters[$getParameterKey]) && is_array($configuratedParameter)){ | |
$result = $this->shouldCheck($configuratedParameter, $getParameters[$getParameterKey]); | |
} elseif (is_string($configuratedParameter) && $configuratedParameter !== 0 && isset($getParameters[$getParameterKey])){ | |
$result = [ | |
'table' => $configuratedParameter, | |
'recordUid' => $getParameters[$getParameterKey], | |
]; | |
} | |
} | |
} | |
return $result; | |
} | |
/** | |
* @return \TYPO3\CMS\Core\Database\DatabaseConnection | |
*/ | |
protected function getDatabaseConnection() | |
{ | |
return $GLOBALS['TYPO3_DB']; | |
} | |
/** | |
* @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController | |
*/ | |
protected function getTypoScriptFrontendController() | |
{ | |
return $GLOBALS['TSFE']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works fine (TYPO3 6.2 and tx_news 3.2.8) for untranslated records, but unfortunately USERDEF1/USERDEF2 ist not set if the translated record does NOT exist in the standard language. Can you confirm this?