Created
September 13, 2016 14:09
-
-
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
This file contains hidden or 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 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