Skip to content

Instantly share code, notes, and snippets.

@birger-fuehne
Forked from mhuber84/MenuUtility.php
Created January 13, 2017 08:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save birger-fuehne/a8a97c94ec9346d691174462ccbfcfcc to your computer and use it in GitHub Desktop.
Save birger-fuehne/a8a97c94ec9346d691174462ccbfcfcc 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'];
}
}
@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?
The other thing I do not understand ist this "# GET.PARAMETER.WITH.RECORD.UID = TABLENAME" what do oyu mean with this line?

Thanks a lot and have a nice day

@rbqueo
Copy link

rbqueo commented May 15, 2020

Thanks for the work, this saved me some hours. I forked it to work with TYPO3 9 https://gist.github.com/rbqueo/1a6e4d0e80df4a371ea39a287b57650f

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