-1

I've checked Debug plugin discovery and tried restarting Apache and setting $settings['class_loader_auto_detect'] = FALSE; in settings.php, with no change.

I have a custom module. Setting a breakpoint anywhere that I know should fire works, including the config form for my module.

The module defines a plugin of the annotated type, and I cannot get any code in the plugin manager to fire. I also note that I've used a contrib module as a model, and cannot get code in its plugin manager to break either.

If I refer to the module as 'x', in x/src/XManager.php I have:

namespace Drupal\x;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\x\Annotation\X;

/**
 * Class XManager
 *
 * @package \Drupal\x
 */
class XManager extends DefaultPluginManager {

  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    $subdir = 'Plugin/X';
    $plugin_interface = XInterface::class;
    $plugin_definition_annotation_name = X::class;
    parent::__construct($subdir, $namespaces, $module_handler, $plugin_interface, $plugin_definition_annotation_name);
    $this->alterInfo('x_info');
    $this->setCacheBackend($cache_backend, 'x_plugins');
  }
  
}

and in x/src/Annotation/X.php

namespace Drupal\x\Annotation;

use Drupal\Component\Annotation\Plugin;

/**
 * @see plugin_api
 *
 * @Annotation
 */
class X extends Plugin {

  /**
   * The plugin ID.
   *
   * @var string
   */
  public $id;

  /**
   * The plugin label.
   *
   * @var \Drupal\Core\Annotation\Translation
   *
   * @ingroup plugin_translatable
   */
  public $label;

  /**
   * The plugin field type IDs.
   *
   * @var array
   */
  public $field_type_ids = [];

}

and in x/src/Plugin/X/XTest.php

namespace Drupal\x\Plugin\XTest;

/**
 * Class XTest.
 *
 * @X(
 *   id = "x_test",
 *   label = @Translation("X Test"),
 *   field_types = {
 *     "text",
 *     "text_long",
 *     "text_with_summary",
 *   },
 *
 * )
 */
class XTest {

}
3
  • The module defines a plugin of the annotated type, and I cannot get any code in the plugin manager to fire. When do you expect that the code fires? You need to register the class as service and use the service. I also note that I've used a contrib module as a model, and cannot get code in its plugin manager to break either. The discovery process runs only when you install a module or when you clear the plugin discovery cache.
    – 4uk4
    Feb 1, 2021 at 7:58
  • Thank you! You’re right. The service request was badly placed. BTW - I’ve credited you for earlier help: theaccidentalcoder dotcom /about/site/credits
    – JAyenGreen
    Feb 2, 2021 at 2:05
  • Please paste as an answer.
    – JAyenGreen
    Feb 2, 2021 at 2:06

1 Answer 1

1

To debug a plugin manager use it as a service and get the discovered plugins:

$type = \Drupal::service('plugin.manager.archiver');
$plugin_definitions = $type->getDefinitions();

This gets the plugin definitions from cache. If you want to rerun the discovery process because you have added or modified a plugin annotation clear the entire Drupal cache or only the plugin manager specific one:

$type->clearCachedDefinitions();

Reference: https://www.drupal.org/docs/drupal-apis/plugin-api/creating-your-own-plugin-manager

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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