Skip to content

Support persistent event listeners in core/components/*/bootstrap.php and allow anonymous functions #16768

@OlegAnTo2000

Description

@OlegAnTo2000

Feature request

Summary

Support persistent event listeners registered by $modx->addEventListener($eventname, $pluginId) in bootstrap.php files and allow attaching anonymous functions as event handlers.

Why is it needed?

In MODX 3, packages can use bootstrap.php for initialization, which is very convenient. However:

  • When adding events with $modx->addEventListener($event, $pluginId) inside bootstrap.php, they do not work as expected because later the eventMap is reloaded and overwrites these changes.
  • This forces developers to always create plugins even for simple cases, which adds unnecessary boilerplate.

Having the ability to:

  1. Register persistent events directly in bootstrap.php.
  2. Attach closures or callable functions as event handlers.

…would make package development cleaner and more flexible.

Suggested solution(s)

  1. Ensure that event listeners registered in bootstrap.php are not removed/overwritten when the eventMap is reinitialized.
  2. Extend $modx->addEventListener() to support callbacks/closures, for example:
$modx->addEventListener('OnWebPageInit', function($modx, $event) {
    // custom logic here
});

or create new method like addEventListenerClosure.

This would allow developers to bind logic directly in code without creating a separate plugin element.

Related issue(s)/PR(s)

Not found yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureRequest about implementing a brand new function or possibility.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions