Skip to content

Instantly share code, notes, and snippets.

@wazum
Created July 22, 2020 10:51
Show Gist options
  • Save wazum/c069aad38a0be77c9be2c0a941d6dba1 to your computer and use it in GitHub Desktop.
Save wazum/c069aad38a0be77c9be2c0a941d6dba1 to your computer and use it in GitHub Desktop.
ViewHelper for language menu and news
<f:section name="LanguageMenu">
<f:if condition="({menu.0.active} && {menu.1.available} && {n:translationAvailable(languageId: 1)}) || ({menu.1.active} && {menu.0.available} && {n:translationAvailable(languageId: 0)})">
<div class="language-switch">
<ul>
<?php
declare(strict_types=1);
namespace Vendor\Extension\ViewHelpers;
use GeorgRinger\News\Domain\Repository\NewsRepository;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
use TYPO3\CMS\Core\Context\LanguageAspect;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
class TranslationAvailableViewHelper extends AbstractViewHelper
{
public function initializeArguments(): void
{
$this->registerArgument('languageId', 'int', 'Lanuage ID', true);
}
/**
* Returns true if the current view is a news detail view
* and there's a translation available for the current news.
*
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return bool
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
): bool {
if (isset($GLOBALS['_GET']['tx_news_pi1']['news'], $GLOBALS['_GET']['tx_news_pi1']['action']) &&
$GLOBALS['_GET']['tx_news_pi1']['action'] === 'detail') {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('tx_news_domain_model_news');
// Default language
if ((int)$arguments['languageId'] === 0) {
return (bool)$queryBuilder->count('uid')
->from('tx_news_domain_model_news')
->where(
$queryBuilder->expr()->eq('uid', (int)$GLOBALS['_GET']['tx_news_pi1']['news'])
)
->execute()
->fetchColumn();
}
return (bool)$queryBuilder->count('uid')
->from('tx_news_domain_model_news')
->where(
$queryBuilder->expr()->eq('l10n_parent', (int)$GLOBALS['_GET']['tx_news_pi1']['news'])
)
->execute()
->fetchColumn();
}
// Something else than a news record, show the language menu
return true;
}
/**
* @return TypoScriptFrontendController
*/
protected static function getTypoScriptFrontendController(): TypoScriptFrontendController
{
return $GLOBALS['TSFE'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment