88 lines
2.9 KiB
PHP
88 lines
2.9 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\Be;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Evoweb\EwBloggy\Domain\Model\Post;
|
|
use TYPO3\CMS\Backend\Routing\UriBuilder;
|
|
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
|
|
|
|
class PostViewHelper extends AbstractTagBasedViewHelper
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $tagName = 'a';
|
|
|
|
public function __construct(readonly private UriBuilder $uriBuilder)
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function initializeArguments(): void
|
|
{
|
|
parent::initializeArguments();
|
|
|
|
$this->registerArgument('post', Post::class, 'The post to link to', true);
|
|
$this->registerArgument('returnUri', 'bool', 'return only uri', false, false);
|
|
$this->registerArgument('action', 'string', 'action to link', false, null);
|
|
}
|
|
|
|
public function render(): string
|
|
{
|
|
$request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
|
|
if (!$request instanceof ServerRequestInterface) {
|
|
throw new \RuntimeException(
|
|
'ViewHelper ew:link.be.post needs a request implementing ServerRequestInterface.',
|
|
1684305293
|
|
);
|
|
}
|
|
|
|
/** @var Post $post */
|
|
$post = $this->arguments['post'];
|
|
|
|
switch ($this->arguments['action']) {
|
|
case 'edit':
|
|
$uri = (string)$this->uriBuilder->buildUriFromRoute('record_edit', [
|
|
'edit' => ['pages' => [$post->getUid() => 'edit']],
|
|
'returnUrl' => $request->getAttribute('normalizedParams')->getRequestUri(),
|
|
]);
|
|
break;
|
|
default:
|
|
$uri = (string)$this->uriBuilder->buildUriFromRoute('web_layout', [
|
|
'id' => $post->getUid(),
|
|
'returnUrl' => $request->getAttribute('normalizedParams')->getRequestUri(),
|
|
]);
|
|
break;
|
|
}
|
|
|
|
if (isset($this->arguments['returnUri']) && $this->arguments['returnUri'] === true) {
|
|
return htmlspecialchars($uri, ENT_QUOTES | ENT_HTML5);
|
|
}
|
|
|
|
$linkText = $this->renderChildren() ?? (
|
|
$post->getTitle() !== ''
|
|
? $post->getTitle()
|
|
: LocalizationUtility::translate('backend.message.nopost', 'blog')
|
|
);
|
|
$this->tag->addAttribute('href', $uri);
|
|
$this->tag->setContent($linkText);
|
|
|
|
return $this->tag->render();
|
|
}
|
|
}
|