Skip to content

Instantly share code, notes, and snippets.

@mhuber84
Last active October 18, 2018 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mhuber84/dce2b09f5ef17ec596bdd938f56811ff to your computer and use it in GitHub Desktop.
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
<?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'];
}
}
@mhuber84
Copy link
Author

Example: On a news detail page the language menu items should not be shown for languages the current news record is not translated to.

#TYPO3

@SIlverback-Web
Copy link

Hi,
thank you very much for submitting this script. Unfortunately I am having troubles in making it work on one of my (TYPO3 7.6.23) sites. I must admit that I do not have much of experience as far as utilities like this are concerned. Can you tell me where I have to save the MenuUtility.php file? Do i have to "declare" or "register" it somewhere too?

Thanks a lot and have a nice day

@bluecookiemonster007
Copy link

bluecookiemonster007 commented Oct 18, 2018

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?

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