Feature-91132-IntroduceUserSettingsJavaScriptModulesEvent.rst 2.34 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
.. include:: ../../Includes.txt

==================================================================
Feature: #91132 - Introduce User Settings JavaScript Modules Event
==================================================================

See :issue:`91132`

Description
===========

JavaScript events in custom User Settings Configuration options shall
not be placed as inline JavaScript anymore, but utilize a dedicated
JavaScript module to handle custom events
15
(see :doc:`Important-91132-AvoidJavaScriptInUserSettingsConfigurationOptions`)
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

This new PSR-14 event is introduced:

* :php:`\TYPO3\CMS\SetupEvent\AddJavaScriptModulesEvent`

These public methods are exposed:

* :php:`public function addModule(string $moduleName): void`
* :php:`public function getModules(): array`

:php:`$moduleName` refers to the JavaScript module to be loaded with RequireJS
(e.g. `TYPO3/CMS/MyExtension/CustomUserSettingsModule`).


Example
=======

A listener using mentioned PSR-14 event could look like the following.

.. rst-class:: bignums

   1. Register listener

      :file:`typo3conf/my-extension/Configuration/Services.yaml`

      .. code-block:: yaml

         services:
            MyVendor\MyExtension\EventListener\CustomUserSettingsListener:
             tags:
               - name: event.listener
                 identifier: 'myExtension/CustomUserSettingsListener'
                 event: TYPO3\CMS\SetupEvent\AddJavaScriptModulesEvent


   2. Implement Listener to load JavaScript module `TYPO3/CMS/MyExtension/CustomUserSettingsModule`

      .. code-block:: php

         namespace MyVendor\MyExtension\EventListener;

         use TYPO3\CMS\SetupEvent\AddJavaScriptModulesEvent;

         class CustomUserSettingsListener
         {
             // name of JavaScript module to be loaded
             private const MODULE_NAME = 'TYPO3/CMS/MyExtension/CustomUserSettingsModule';

             public function __invoke(AddJavaScriptModulesEvent $event): void
             {
                 $javaScriptModuleName = 'TYPO3/CMS/MyExtension/CustomUserSettings';
                 if (in_array(self::MODULE_NAME, $event->getModules(), true)) {
                     return;
                 }
                 $event->addModule(self::MODULE_NAME);
             }
         }


Related
=======

78
- :doc:`Important-91132-AvoidJavaScriptInUserSettingsConfigurationOptions`
79
80

.. index:: PHP-API, ext:core