I want to load a plugin definition by its string id within a controller extending ControllerBase. What is the relevant service or class I am looking for?

1 Answer 1


You can use the plugin manager to fetch a plugin. You only need to find the correct plugin manager service:

$type = \Drupal::service('plugin.manager.service'); // Fill your own plugin manager service.
$plugin_definition = $type->getDefinition('plugin_id'); // Fill needed plugin id.
  • 1
    Ahh yeah, it hit me right after I posted it. I was having trouble locating the plugin manager. I injected it to the controller via $container->get('plugin.manager.media_entity.type') and now I can use it.
    – Kevin
    Commented Sep 19, 2017 at 16:01
  • Is there a way to return the plugin configuration as well?
    – Kevin
    Commented Sep 19, 2017 at 16:13
  • I would not know, sorry. Though I would think it should be possible. Can't you access it throught the config service? Perhaps there is a pattern? Commented Sep 19, 2017 at 16:25
  • Yeah it looks like I could just use the config service, when I thought I would have to look at the plugin definition. Oh well! Now I know anyway.
    – Kevin
    Commented Sep 19, 2017 at 16:45
  • 1
    The plugin configuration is part of the media type/bundle config entity. cgit.drupalcode.org/media_entity/tree/src/Entity/…, it has getType() to get the plugin, which has getPluginDefinition() and getConfiguration(), you can also use getTypeConfiguration() directly on the media bundle entity
    – Berdir
    Commented Sep 19, 2017 at 19:23

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.