ew_bloggy/Classes/ViewHelpers/Link/ArchiveViewHelper.php
2025-01-21 20:22:57 +01:00

77 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
/*
* This file is developed by evoWeb.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/
namespace Evoweb\EwBloggy\ViewHelpers\Link;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
class ArchiveViewHelper extends AbstractTagBasedViewHelper
{
protected $tagName = 'a';
public function __construct(protected UriBuilder $uriBuilder)
{
parent::__construct();
}
public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerArgument('month', 'int', 'The month to link to');
$this->registerArgument('year', 'int', 'The year to link to', true);
}
public function render(): string
{
$request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
$year = (int)$this->arguments['year'];
$month = (int)$this->arguments['month'];
// @todo migrate to site settings
$pageUid = (int)($request->getAttribute('frontend.typoscript')
->getSetupTree()
->getChildByName('plugin')
?->getChildByName('tx_ewbloggy')
?->getChildByName('settings')
?->getChildByName('archiveUid')
?->getValue() ?? 0);
$arguments = [
'year' => $year
];
if ($month > 0) {
$arguments['month'] = $month;
}
$this->uriBuilder->reset()
->setRequest($request)
->setTargetPageUid($pageUid);
$uri = $this->uriBuilder->uriFor('listPostsByDate', $arguments, 'Post', 'EwBloggy', 'Archive');
$linkText = $this->renderChildren() ?? implode('-', $arguments);
if ($uri !== '') {
$this->tag->addAttribute('href', $uri);
$this->tag->setContent((string)$linkText);
$result = $this->tag->render();
} else {
$result = $linkText;
}
return (string)$result;
}
}