Last active
September 12, 2024 11:50
-
-
Save mtness/660c61850eeb20b2591d438be5385ca6 to your computer and use it in GitHub Desktop.
This file contains 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 MarkusTimtner\Sitepackage\ViewHelpers; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
use TYPO3\CMS\Core\Database\ConnectionPool; | |
use TYPO3\CMS\Core\Database\Connection; | |
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; | |
/** | |
* ViewHelper to show the page.media.fileObject of the default language | |
* goes along with the MenuSupagesImages | |
* | |
* <code> | |
* <p:pagemedia uid='{page.data.l10n_parent}' /> | |
* </code> | |
* or inline: | |
* <code> | |
* {p:pagemedia(uid: '{page.data.l10n_parent}')} | |
* </code> | |
* | |
* usage in template: | |
* <f:if condition="{p:pagemedia(uid: '{page.data.l10n_parent}')}"> | |
* <f:image src="{p:pagemedia(uid: '{page.data.l10n_parent}')}" treatIdAsReference="1" /> | |
* </f:if> | |
*/ | |
class PagemediaViewHelper extends AbstractViewHelper { | |
/** | |
* As this ViewHelper renders HTML, the output must not be escaped. | |
* | |
* @var bool | |
*/ | |
protected $escapeOutput = false; | |
/** | |
* Initialize arguments | |
*/ | |
public function initializeArguments() | |
{ | |
parent::initializeArguments(); | |
$this->registerArgument('uid', 'int', 'page uid', true); | |
} | |
public function render() { | |
$uid = $this->arguments['uid']; | |
$fileRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\FileRepository::class); | |
$fileObjects = $fileRepository->findByRelation('pages', 'media', $uid); | |
if (!empty($fileObjects[0])) { | |
$file = $fileObjects[0]->getPublicUrl(); | |
return (string)$file; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment