AddJavaScriptModulesEvent

JavaScript events in custom user settings configuration options should not be placed as inline JavaScript. Instead, use a dedicated JavaScript module to handle custom events.

Example

EXT:my_extension/Classes/UserSettings/EventListener/MyEventListener.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\UserSettings\EventListener;

use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Setup\Event\AddJavaScriptModulesEvent;

#[AsEventListener(
    identifier: 'my-extension/my-event-listener'
)]
final class MyEventListener
{
    // The name of JavaScript module to be loaded
    private const MODULE_NAME = 'TYPO3/CMS/MyExtension/CustomUserSettingsModule';

    public function __invoke(AddJavaScriptModulesEvent $event): void
    {
        if (in_array(self::MODULE_NAME, $event->getModules(), true)) {
            return;
        }
        $event->addModule(self::MODULE_NAME);
    }
}

New in version 13.0: The PHP attribute \TYPO3\CMS\Core\Attribute\AsEventListener has been introduced to tag a PHP class as an event listener. Alternatively, or if you need to be compatible with older TYPO3 versions, you can also register an event listener via the Configuration/Services.yaml file. Switch to an older version of this page for an example or have a look at the section Implementing an event listener in your extension.

API

class TYPO3\CMS\Setup\Event\AddJavaScriptModulesEvent

Collects additional JavaScript modules to be loaded in SetupModuleController.

addJavaScriptModule(string $specifier)
Parameters
  • $specifier (string) -- Bare module identifier like @my/package/filename.js

getJavaScriptModules()
Return type

array

Returns

string[]