1

I'm porting over some Drupal 7 code to 8 and am stuck with one thing. In D7, I built some custom modules with a special hook function that would provide some debug information. To get a list of them, I can call module_implements('debugInfo'). For D8, I want to search, starting in the modules/custom directory, and return a list of modules with a class that has a public member function named 'debugInfo'. Is there a way to do this? I found an example, the first answer at PHP - get all class names inside a particular namespace, but if I have a base class with the method I'm looking for, it also reports all child classes inheriting the base class.

Since I'm only concerned with my custom modules, I can't use composer to do this. What I want is a way to get a dictionary of implemented classes from a starting directory, and get a list of available methods from the classes.

pglatz
  • 143
  • 4

1 Answers1

0

Sounds like you probably want a custom plugin manager.

In Drupal 8 (and now 9) instead of implementing custom hooks and searching, you can instead create a plugin manager service and then implement plugins in each module to call the information when you need it. The manager service will automatically discover the plugins and you can call on them as appropriate.

To create a plugin is a bit more work than custom hooks were but also more flexible and gives you options. These resources can help you work through the details:

To help you short cut you're reading a little: start with an Annotated plugin.

acrosman
  • 12,814
  • 10
  • 39
  • 55
  • Thanks for the excellent response with examples, it really helps me to understand what's going on. I'm finding the transition to Drupal 8 to be pretty strait forward, but sometimes it's difficult to get my head around some of the concepts when moving to more of an ob sect oriented approach. I was thinking of making an event listener to do this, but the plugins look like a better way. Thanks again, this looks like fun. – pglatz Nov 18 '20 at 19:05