ew_base/Classes/ViewHelpers/Be/ThumbnailViewHelper.php
2024-04-28 18:49:46 +02:00

108 lines
3.6 KiB
PHP
Executable File

<?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\EwBase\ViewHelpers\Be;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Type\Bitmask\Permission;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
class ThumbnailViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerArgument('row', 'array', 'content data', true);
$this->registerArgument('tableName', 'string', 'table name', true);
$this->registerArgument('fieldName', 'string', 'field name', true);
}
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
): string {
$row = $arguments['row'];
$tableName = $arguments['tableName'];
$fieldName = $arguments['fieldName'];
return self::linkEditContent(
BackendUtility::thumbCode(row: $row, table: $tableName, field: $fieldName, linkInfoPopup: false),
$row
);
}
protected static function linkEditContent(string $linkText, $row): string
{
if (empty($linkText)) {
return $linkText;
}
$backendUser = self::getBackendUser();
if (
$backendUser->check('tables_modify', 'tt_content')
&& $backendUser->recordEditAccessInternals('tt_content', $row)
&& (
new Permission(
$backendUser->calcPerms(BackendUtility::getRecord('pages', $row['pid']) ?? [])
)
)->editContentPermissionIsGranted()
) {
$urlParameters = [
'edit' => [
'tt_content' => [
$row['uid'] => 'edit',
],
],
'returnUrl' => self::getRequest()->getAttribute('normalizedParams')->getRequestUri()
. '#element-tt_content-' . $row['uid'],
];
/** @var UriBuilder $uriBuilder */
$uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
$url = (string)$uriBuilder->buildUriFromRoute('record_edit', $urlParameters);
return '<a href="' . htmlspecialchars($url) . '" title="'
. htmlspecialchars(self::getLanguageService()->sL(
'LLL:EXT:typo3/sysext/backend/Resources/Private/Language/locallang_layout.xlf:edit'
))
. '">' . $linkText . '</a>';
}
return $linkText;
}
protected static function getBackendUser(): BackendUserAuthentication
{
return $GLOBALS['BE_USER'];
}
protected static function getLanguageService(): LanguageService
{
return $GLOBALS['LANG'];
}
protected static function getRequest(): ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'];
}
}