Feature: #108524 - Configuration file to register global Fluid namespaces
See forge#108524
Description
The extension-level configuration file Configuration/
is introduced, which enables a structured way to register and extend global
Fluid namespaces. This replaces the old configuration in
$GLOBALS, see
deprecation.
Example:
<?php
return [
'myext' => ['MyVendor\\MyExtension\\ViewHelpers'],
'mycmp' => ['MyVendor\\MyExtension\\Components'],
];
Overriding existing ViewHelpers
TYPO3 reads and merges Configuration/ files from all
loaded extensions in the usual loading order, which can be manipulated by
declaring dependencies in composer. and possibly ext_. If an
extension registers a namespace that has already been registered by another
extension, these namespaces will be merged by Fluid. This allows extensions
to override ViewHelpers of another extension selectively.
Example (extension2 depends on extension1):
<?php
return [
'myext' => ['MyVendor\\MyExtension1\\ViewHelpers'],
];
<?php
return [
'myext' => ['MyVendor\\MyExtension2\\ViewHelpers'],
];
Resulting namespace definition:
[
'myext' => [
'MyVendor\\MyExtension1\\ViewHelpers',
'MyVendor\\MyExtension2\\ViewHelpers',
],
];
Namespaces are processed in reverse order, which means that
<myext: would first check for
EXT:, and would
fall back to EXT:.
PSR-14 event to modify namespaces
The new
\Modify is
introduced, which allows modification of the whole namespaces array
before it is being passed to Fluid. This allows for example to:
- completely redefine an existing namespace (instead of extending it)
- add namespaces conditionally
- modify order of merged namespaces
Example:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\EventListener;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Fluid\Event\ModifyNamespacesEvent;
#[AsEventListener]
final readonly class ModifyNamespacesListener
{
public function __invoke(ModifyNamespacesEvent $event): void
{
$namespaces = $event->getNamespaces();
// Replace existing "theme" namespace completely
$namespaces['theme'] = ['MyVendor\\MyExtension\\ViewHelpers'];
$event->setNamespaces($namespaces);
}
}
Note that namespaces might still be imported locally from within a template file, which is unaffected by this event.
Backwards-compatible namespaces in extensions
There are several ways to provide backwards-compatible global namespaces in extensions, depending on the concrete use case:
- preferred: Define namespace in
TYPO3_(with version check) and in newCONF_ VARS Namespaces.. This means that in TYPO3 v14 installations the newphp Namespaces.can already be used to extend the namespace.php - alternative: Define namespace both in
TYPO3_(without version check) and in newCONF_ VARS Namespaces.. This means however that the namespace can only be extended withphp TYPO3_, not with the newCONF_ VARS Namespaces..php - keep
TYPO3_until support for < v14 is dropped by the extension. This can only be extended withCONF_ VARS TYPO3_as well.CONF_ VARS - implement own merging logic in
Modifyif necessary.Namespaces Event
Example for preferred option:
<?php
return [
'myext' => ['MyVendor\\MyExtension\\ViewHelpers'],
];
<?php
if ((new \TYPO3\CMS\Core\Information\Typo3Version())->getMajorVersion() < 14) {
$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['myext'][] = 'MyVendor\\MyExtension\\ViewHelpers';
}
To sum up:
Namespaces.can only extend namespaces defined inphp Namespaces..php TYPO3_can extend bothCONF_ VARS TYPO3_andCONF_ VARS Namespaces..php Modifycan modify everything.Namespaces Event
Impact
Extensions can now register global Fluid namespaces in a dedicated
configuration file Configuration/. The old
TYPO3_ registration can be used for backwards compatibility.