Skip to content

Commit b885637

Browse files
committed
[TASK] Adapt extbase view resolver to TYPO3 13
Remove the outdated extbase view adapter and allow users to override extbase views with custom processors.
1 parent 46dfc35 commit b885637

14 files changed

+187
-184
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace PrototypeIntegration\PrototypeIntegration\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
8+
9+
class AsExtbaseProcessorCompilerPass implements CompilerPassInterface
10+
{
11+
public function process(ContainerBuilder $container): void
12+
{
13+
try {
14+
$definition = $container->findDefinition('PrototypeIntegration\PrototypeIntegration\View\ExtbaseProcessorRegistry');
15+
} catch (ServiceNotFoundException $e) {
16+
return;
17+
}
18+
19+
foreach ($container->findTaggedServiceIds('pti.extbase_processors') as $processorClassname => $tags) {
20+
$container->getDefinition($processorClassname)->setPublic(true);
21+
foreach ($tags as $attributes) {
22+
$definition->addMethodCall('addOverride', [
23+
$attributes['controller'],
24+
$attributes['action'],
25+
$processorClassname,
26+
$attributes['template'],
27+
]);
28+
}
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace PrototypeIntegration\PrototypeIntegration\DependencyInjection\Attribute;
4+
5+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
6+
class AsExtbaseProcessor
7+
{
8+
public function __construct(
9+
public string $controller,
10+
public string $action,
11+
public ?string $template = null,
12+
) {
13+
}
14+
}

Classes/View/DefaultViewResolver.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
88
use TYPO3\CMS\Core\Utility\GeneralUtility;
9-
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
9+
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
1010
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
1111

1212
class DefaultViewResolver implements ViewResolverInterface
@@ -29,14 +29,11 @@ public function getViewForContentObject(
2929
}
3030

3131
public function getViewForExtbaseAction(
32-
string $controllerObjectName,
33-
string $actionName,
34-
string $format,
35-
?string $template
32+
RequestInterface $extbaseRequest,
33+
?string $template = null,
3634
): PtiViewInterface {
3735
// Allow the CompoundProcessor to force json output
38-
$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
39-
$contentObject = $configurationManager->getContentObject();
36+
$contentObject = $extbaseRequest->getAttribute('currentContentObject');
4037

4138
$ptiForceView = $contentObject?->data['_pti_format'] ?? null;
4239
if (
@@ -58,6 +55,7 @@ public function getViewForExtbaseAction(
5855
if (isset($format) && $format === 'json') {
5956
return $this->getJsonView();
6057
}
58+
6159
return $this->getDefaultView($template);
6260
}
6361

Classes/View/Event/ExtbaseViewAdapterVariablesConvertedEvent.php

-25
This file was deleted.
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace PrototypeIntegration\PrototypeIntegration\View;
4+
5+
class ExtbaseProcessorRegistry
6+
{
7+
public function __construct(
8+
protected array $overrides = [],
9+
) {
10+
}
11+
12+
public function addOverride(
13+
string $controller,
14+
string $action,
15+
string $processorClassName,
16+
?string $template = null,
17+
) {
18+
$this->overrides[$controller][$action] = [
19+
'processors' => [$processorClassName],
20+
'template' => $template,
21+
];
22+
}
23+
24+
public function getOverrides(): array
25+
{
26+
return $this->overrides;
27+
}
28+
29+
/** array<processors: string[], template?: ?string> */
30+
public function getProcessorForControllerAndAction(string $controller, string $action): ?array
31+
{
32+
return $this->overrides[$controller][$action] ?? null;
33+
}
34+
}

Classes/View/ExtbaseViewAdapter.php

-88
This file was deleted.

Classes/View/ExtbaseViewAdapterContext.php

-28
This file was deleted.

Classes/View/ExtbaseViewAdapterContextAware.php

-8
This file was deleted.

Classes/View/JsonView.php

-12
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace PrototypeIntegration\PrototypeIntegration\View;
66

7-
use TYPO3\CMS\Core\Utility\Exception\NotImplementedMethodException;
8-
97
class JsonView implements PtiViewInterface
108
{
119
protected array $variables = [];
@@ -19,14 +17,4 @@ public function setVariables($variables): void
1917
{
2018
$this->variables = $variables;
2119
}
22-
23-
public function renderSection($sectionName, array $variables = [], $ignoreUnknown = false)
24-
{
25-
throw new NotImplementedMethodException('renderSection not implemented for JsonView', 1690795638213);
26-
}
27-
28-
public function renderPartial($partialName, $sectionName, array $variables, $ignoreUnknown = false)
29-
{
30-
throw new NotImplementedMethodException('renderSection not implemented for JsonView', 1690795646994);
31-
}
3220
}

Classes/View/ViewAdapter.php

+11-12
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
namespace PrototypeIntegration\PrototypeIntegration\View;
66

77
use PrototypeIntegration\PrototypeIntegration\Processor\PtiDataProcessor;
8-
use TYPO3\CMS\Core\Utility\Exception\NotImplementedMethodException;
9-
use TYPO3Fluid\Fluid\View\AbstractView;
10-
use TYPO3Fluid\Fluid\View\ViewInterface;
8+
use TYPO3\CMS\Core\View\ViewInterface;
119

12-
class ViewAdapter extends AbstractView implements ViewInterface
10+
class ViewAdapter implements ViewInterface
1311
{
1412
protected ?array $settings;
1513

14+
protected array $variables = [];
15+
1616
/**
1717
* @param PtiDataProcessor[] $dataProcessors
1818
*/
@@ -22,10 +22,7 @@ public function __construct(
2222
) {
2323
}
2424

25-
/**
26-
* @return string The rendered view
27-
*/
28-
public function render()
25+
public function render(string $templateFileName = ''): string
2926
{
3027
$variables = $this->variables;
3128
foreach ($this->dataProcessors as $dataProcessor) {
@@ -39,13 +36,15 @@ public function render()
3936
return $this->view->render();
4037
}
4138

42-
public function renderSection($sectionName, array $variables = [], $ignoreUnknown = false)
39+
public function assign(string $key, mixed $value): self
4340
{
44-
throw new NotImplementedMethodException('', 1691406217099);
41+
$this->variables[$key] = $value;
42+
return $this;
4543
}
4644

47-
public function renderPartial($partialName, $sectionName, array $variables, $ignoreUnknown = false)
45+
public function assignMultiple(array $values): self
4846
{
49-
throw new NotImplementedMethodException('', 1691406221493);
47+
$this->variables = array_replace_recursive($this->variables, $values);
48+
return $this;
5049
}
5150
}

0 commit comments

Comments
 (0)