Commit 05e679a7 authored by Thomas Löffler's avatar Thomas Löffler Committed by Benni Mack

[BUGFIX] Use the correct PreviewRenderer as fallback

When there is no specific preview renderer defined for
a given subtype, the type-specific renderer can only
be used if it's not an array. Otherwise, the default
preview renderer defined in TCA crtl should be used.

Releases: master, 10.4
Resolves: #92758
Change-Id: Ia88d262f52ddac174cbe6750c591898c84872a89
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/66413Tested-by: Oliver Bartsch's avatarOliver Bartsch <bo@cedev.de>
Tested-by: default avatarTYPO3com <noreply@typo3.com>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Reviewed-by: Oliver Bartsch's avatarOliver Bartsch <bo@cedev.de>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
parent 6e99d31d
......@@ -51,17 +51,19 @@ class StandardPreviewRendererResolver implements PreviewRendererResolverInterfac
$typeConfiguration = $tca['types'][$tcaTypeOfRow] ?? [];
$subTypeValueField = $typeConfiguration['subtype_value_field'] ?? null;
if (!empty($subTypeValueField) && !empty($typeConfiguration['previewRenderer']) && is_array($typeConfiguration['previewRenderer'])) {
// An array of subtype_value_field indexed preview renderers was defined, look up the right
// class to use for the sub-type defined in this $row.
$previewRendererClassName = $typeConfiguration['previewRenderer'][$row[$subTypeValueField]] ?? null;
}
if (!empty($typeConfiguration['previewRenderer'])) {
if (!empty($subTypeValueField) && is_array($typeConfiguration['previewRenderer'])) {
// An array of subtype_value_field indexed preview renderers was defined, look up the right
// class to use for the sub-type defined in this $row.
$previewRendererClassName = $typeConfiguration['previewRenderer'][$row[$subTypeValueField] ?? ''] ?? null;
}
// If no class was found in the subtype_value_field
if (!$previewRendererClassName && !empty($typeConfiguration['previewRenderer'])) {
// A type-specific preview renderer was configured for the TCA type (and one was not detected
// based on the higher-priority lookups above).
$previewRendererClassName = $typeConfiguration['previewRenderer'];
// If no class was found in the subtype_value_field
if (!$previewRendererClassName && !is_array($typeConfiguration['previewRenderer'])) {
// A type-specific preview renderer was configured for the TCA type (and one was not detected
// based on the higher-priority lookups above).
$previewRendererClassName = $typeConfiguration['previewRenderer'];
}
}
}
......
<?php
declare(strict_types=1);
/*
* This file is part of a TYPO3 extension.
*
* 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.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Backend\Tests\Unit\Preview;
use TYPO3\CMS\Backend\Preview\PreviewRendererInterface;
use TYPO3\CMS\Backend\Preview\StandardContentPreviewRenderer;
use TYPO3\CMS\Backend\Preview\StandardPreviewRendererResolver;
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
class StandardPreviewRendererResolverTest extends UnitTestCase
{
protected StandardPreviewRendererResolver $subject;
protected function setUp(): void
{
parent::setUp();
$this->subject = new StandardPreviewRendererResolver();
$GLOBALS['TCA']['tt_content'] = [
'ctrl' => [
'type' => 'CType',
'previewRenderer' => StandardContentPreviewRenderer::class,
],
'types' => [
'list' => [
'subtype_value_field' => 'list_type'
]
]
];
}
protected function tearDown(): void
{
GeneralUtility::purgeInstances();
parent::tearDown();
}
/**
* @test
*/
public function resolveStandardContentPreviewRenderer(): void
{
$table = 'tt_content';
$row = [
'CType' => 'text'
];
self::assertEquals(
StandardContentPreviewRenderer::class,
get_class($this->subject->resolveRendererFor($table, $row, 0))
);
}
/**
* @test
*/
public function resolveCustomContentPreviewRenderer(): void
{
$customPreviewRenderer = $this->getMockBuilder(PreviewRendererInterface::class)->getMock();
$table = 'tt_content';
$row = [
'CType' => 'list',
'list_type' => 'custom'
];
$GLOBALS['TCA'][$table]['types']['list']['previewRenderer']['custom'] = get_class($customPreviewRenderer);
self::assertEquals(
get_class($customPreviewRenderer),
get_class($this->subject->resolveRendererFor($table, $row, 0))
);
}
/**
* @test
*/
public function resolveStandardContentPreviewRendererWithCustomPreviewRendererDefined(): void
{
$customPreviewRenderer = $this->getMockBuilder(PreviewRendererInterface::class)->getMock();
$table = 'tt_content';
$row = [
'CType' => 'list',
'list_type' => 'default'
];
$GLOBALS['TCA'][$table]['types']['list']['previewRenderer']['custom'] = get_class($customPreviewRenderer);
self::assertEquals(
StandardContentPreviewRenderer::class,
get_class($this->subject->resolveRendererFor($table, $row, 0))
);
}
/**
* @test
*/
public function resolveStandardContentPreviewRendererWithGeneralPreviewRendererDefinedForAllSubTypes(): void
{
$customPreviewRenderer = $this->getMockBuilder(PreviewRendererInterface::class)->getMock();
$table = 'tt_content';
$row = [
'CType' => 'list',
'list_type' => 'default'
];
$GLOBALS['TCA'][$table]['types']['list']['previewRenderer'] = get_class($customPreviewRenderer);
self::assertEquals(
get_class($customPreviewRenderer),
get_class($this->subject->resolveRendererFor($table, $row, 0))
);
}
/**
* @test
*/
public function getExceptionWithNoPreviewRendererDefined(): void
{
$GLOBALS['TCA']['pages']['ctrl'] = [];
$table = 'pages';
$row = [
'CType' => PageRepository::DOKTYPE_DEFAULT,
];
$this->expectException(\RuntimeException::class);
$this->expectExceptionCode(1477520356);
$this->subject->resolveRendererFor($table, $row, 0);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment