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

Registration of the event listener in the extension's Services.yaml:

EXT:my_extension/Configuration/Services.yaml
services:
  # Place here the default dependency injection configuration

  MyVendor\MyExtension\UserSettings\EventListener\MyEventListener:
    tags:
      - name: event.listener
        identifier: 'my-extension/my-event-listener'
Copied!

Read how to configure dependency injection in extensions.

The corresponding event listener class:

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

declare(strict_types=1);

namespace MyVendor\MyExtension\UserSettings\EventListener;

use TYPO3\CMS\Setup\Event\AddJavaScriptModulesEvent;

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);
    }
}
Copied!

API

class AddJavaScriptModulesEvent
Fully qualified name
\TYPO3\CMS\Setup\Event\AddJavaScriptModulesEvent

Collects additional JavaScript modules to be loaded in SetupModuleController.

addJavaScriptModule ( string $specifier)
param $specifier

Bare module identifier like @my/package/filename.js

addModule ( string $moduleName)

Deprecated: will be removed in TYPO3 v13.0. Use addJavaScriptModule() instead, available since TYPO3 v12.0.

param $moduleName

the moduleName

getJavaScriptModules ( )
Returns
string[]
getModules ( )
Returns
string[]