5

A plugin that I have written is not being discovered.

For eg : https://www.drupal.org/node/2847639

Is there a general suggestion for debugging failing plugin discovery?

4
  • 2
    I just went through a debug for a plugin that wasn't being found a few days ago. Spent hours on it, digging way deep in the code, all the way down to the doctrine level. Turns out that I had a syntax error in the PHP in my plugin class. During discovery, the file is not loaded, but rather read, so if there is a syntax error, it will not fail, it just doesn't find the plugin. So check for syntax errors in your plugin file.
    – Jaypan
    Feb 8, 2017 at 14:28
  • Same with @Jaypan. The first you need check syntax of your plugin. If you know more about code. You can find define plugin by annotations in code and class plugin manager. And better you can say exactly plugin you written.
    – Jonh
    Feb 9, 2017 at 8:55
  • The issues was with a type in the folder name :( I have fixed it now. However I would like to know if there is a better way to debug plugin discovery and first step could be to validate the namespace and folder structure is correct as per the standards.
    – Gokul N K
    Feb 11, 2017 at 11:28
  • Well, the function in question that does the discovery is Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery::getDefinitions() api.drupal.org/api/drupal/…
    – Jaypan
    Feb 11, 2017 at 15:58

2 Answers 2

6
+100

Most of the time it is because the file is not picked up, there are a few common cases for that:

  1. Ensure that the filename matches the class name exactly, case sensitive. Without the extension of course.

  2. Ensure that the namespace matches the folder path exactly, case sensitive.

  3. Restart Apache to clear apcu cache, I've had issues with this when renaming/moving files with a class quite a bit in Drupal 8. Also setting class_loader_auto_detect to FALSE in settings.php should solve this one.

There are even more things to check if you're actually the one providing the plugin type such as the plugin manager.

2
  • I think IDEs should have some basic checks for the first two at-least.
    – Gokul N K
    Feb 15, 2017 at 3:18
  • 1
    One thing is that can happens, you may have two modules with same name, for example: video (custom feature) & video (contrib module). Jun 1, 2017 at 18:32
3

Additionally to the bullet points in the accepted answer...

Ensure that the structure of the comment is correct, not only the layout of the annotation section, but the layout of the comment block itself (including the number of asterisk) is also important. Remembering the Annotation class is itself an annotated class.

I was having the same issue as this question, and ended up with 'print_r' statements littered throughout the getDefinitions() function mentioned in a comment in the first question to determine where it falied. That made me look at my Annotation class and realize the comment block was started with '/*' instead of '/**'

I would put this a comment, but I don't have the required reputation.

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.