0

To main backwards-compatibility I need to define two class aliases in my contrib module. Both of the source and target classes are within the same namespace within my module's namespace.

I placed the class_alias( ) statements at the top of my_module.module in open code, not a function, and this works for everything in the module, and also mostly for everything everywhere. I have created a second module which uses the original classes to check this. The second module has the first as a dependency. This works most of the time, however, there are a few places where I get a 'class not found' error, meaning that the alias statements have not been processed. For example, when viewing a few admin urls e.g admin/config/regional/language

So is there a better place than my_module.module where I can put the class_alias( ) statements, such that they are always processed?

My fall-back solution would be to re-create the two original class files, in the original locations, and each of them have the class alias statement, so that autoloading finds them. But that's a bit untidy and they will cause confusion as they are named incorrectly. It would be nicer to be able to have the two alias statements somewhere, and not have two nearly empty files left around.

2
  • 3
    I don't follow, can you not alias the class names in these files as use Path\To\Class as AliasClass?
    – Kevin
    Commented Mar 8, 2021 at 14:21
  • In reply to Kevin, yes that is my fall-back position, as explained above. I wanted to remove those files and define the aliases somewhere else. Commented Mar 24, 2021 at 18:56

1 Answer 1

1

So is there a better place than my_module.module where I can put the class_alias( ) statements, such that they are always processed?

Put class_alias() in the class file (after the use statements before class {} or at the end of the class file).

*.module files are not loaded by the class loader, they are included later in the kernel bootstrap process.

1
  • Thanks @4k4 yes I discovered that there is not really any other place to put the alises. Useful info about *.module files not being loaded by the class loader but being added later. Commented Mar 24, 2021 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.