Skip to content

Instantly share code, notes, and snippets.

@pixelbrackets
Created September 13, 2016 14:09
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pixelbrackets/28cebf435692c74ffdc2e3af8e9872dc to your computer and use it in GitHub Desktop.
Save pixelbrackets/28cebf435692c74ffdc2e3af8e9872dc to your computer and use it in GitHub Desktop.
TYPO3 ViewHelper to check if a given page is translated into a given language or is translated at all
<?php
namespace Pixelbrackets\AcmeExtension\ViewHelpers;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
/**
* Checks if a given page is translated into a given language or translated at all
*
* = Examples =
*
* <code title="Default">
* <p:pageHasTranslation pageUid="4" sysLanguageUid="2" />
* </code>
* <output>
* true
* </output>
*
* <code title="Is page translated at all, include hidden translations">
* <p:pageHasTranslation pageUid="4" showHiddenTranslations="1" />
* </code>
* <output>
* true
* </output>
*
* <code title="Inline notation">
* {p:pageHasTranslation(pageUid: '4', showHiddenTranslations: 1, sysLanguageUid: '3')}
* </code>
* <output>
* false
* </output>
*
*/
class PageHasTranslationViewHelper extends AbstractViewHelper {
/**
* Get translation information
*
* @param integer $pageUid Uid of the page
* @param boolean $showHiddenTranslations Show hidden translations as well (default FALSE)
* @param integer $sysLanguageUid Check for given language ID (as set in »website language« in root folder)
* @return string
*/
public function render($pageUid = NULL, $showHiddenTranslations = FALSE, $sysLanguageUid = NULL) {
$pageUid = $this->getPageUid($pageUid);
$sysLanguageUid = intval($sysLanguageUid);
$result = FALSE;
// Get all page language overlay records of the selected page
$table = 'pages_language_overlay';
$whereClause = 'pid=' . $pageUid . ' ';
if($sysLanguageUid > 0) {
$whereClause .= 'AND sys_language_uid=' . $sysLanguageUid . ' ';
}
$whereClause .= $GLOBALS['TSFE']->sys_page->enableFields($table, $showHiddenTranslations);
$pageOverlays = $GLOBALS['TYPO3_DB']->exec_SELECTquery('DISTINCT sys_language_uid', $table, $whereClause);
if(FALSE === empty($pageOverlays) && $GLOBALS['TYPO3_DB']->sql_num_rows($pageOverlays) > 0) {
$result = TRUE;
}
return $result;
}
/**
* Get page via pageUid argument or current id
*
* @param integer $pageUid Uid of the page
*
* @return integer
*/
protected function getPageUid($pageUid = NULL) {
if ($pageUid === NULL) {
$pageUid = (integer) $this->renderChildren();
}
if (0 === $pageUid) {
$pageUid = $GLOBALS['TSFE']->id;
}
return (integer) $pageUid;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment