Skip to content

Instantly share code, notes, and snippets.

@wazum
Created June 9, 2020 07:43
Show Gist options
  • Save wazum/00db2e2ac2ce5f36184b4adcab793e2b to your computer and use it in GitHub Desktop.
Save wazum/00db2e2ac2ce5f36184b4adcab793e2b to your computer and use it in GitHub Desktop.
TYPO3 news ViewHelper to add hrDate setting
<?php
namespace Vendor\MyExtension\ViewHelpers;
use GeorgRinger\News\Domain\Model\News;
use GeorgRinger\News\ViewHelpers\LinkViewHelper;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class EventLinkViewHelper extends LinkViewHelper
{
/**
* Render link to news item or internal/external pages.
*
* Adds the hrDate settings to create links with a date.
*
* @return string link
*/
public function render(): string
{
/** @var News $newsItem */
$newsItem = $this->arguments['newsItem'];
$settings = $this->arguments['settings'] ?? [];
$uriOnly = $this->arguments['uriOnly'];
$configuration = $this->arguments['configuration'];
$content = $this->arguments['content'];
ArrayUtility::mergeRecursiveWithOverrule($settings, [
'link' => [
'hrDate' => [
'_typoScriptNodeValue' => 1,
'day' => '',
'month' => 'm',
'year' => 'Y'
]
]
]);
// Rest copied from the original view helper
$tsSettings = (array)$this->pluginSettingsService->getSettings();
ArrayUtility::mergeRecursiveWithOverrule($tsSettings, (array)$settings);
// Options with stdWrap enabled won't override $tsSettings as intended here: override them explicit.
if ($settings['useStdWrap']) {
foreach (GeneralUtility::trimExplode(',', $settings['useStdWrap'], true) as $stdWrapProperty) {
if (is_array($tsSettings[$stdWrapProperty]) && array_key_exists($stdWrapProperty, $settings)) {
$tsSettings[$stdWrapProperty] = $settings[$stdWrapProperty];
}
}
}
$this->init();
$linkedContent = $this->renderChildren();
if ($newsItem === null) {
return $linkedContent;
}
$newsType = (int)$newsItem->getType();
switch ($newsType) {
// internal news
case 1:
$configuration['parameter'] = $newsItem->getInternalurl();
break;
// external news
case 2:
$configuration['parameter'] = $newsItem->getExternalurl();
break;
// normal news record
default:
$configuration = $this->getLinkToNewsItem($newsItem, $tsSettings, $configuration);
}
$url = $this->cObj->typoLink_URL($configuration);
if ($uriOnly) {
return $url;
}
// link could not be generated
if ($url === '' || $linkedContent === $url) {
return $linkedContent;
}
if (isset($tsSettings['link']['typesOpeningInNewWindow'])) {
if (GeneralUtility::inList($tsSettings['link']['typesOpeningInNewWindow'], $newsType)) {
$this->tag->addAttribute('target', '_blank');
}
}
if (!$this->tag->hasAttribute('target')) {
$target = $this->getTargetConfiguration($configuration);
if (!empty($target)) {
$this->tag->addAttribute('target', $target);
}
}
if ($this->hasArgument('section')) {
$url .= '#' . $this->arguments['section'];
}
$this->tag->addAttribute('href', $url);
if (empty($content)) {
$content = $linkedContent;
}
$this->tag->setContent($content);
return $this->tag->render();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment