3

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?

9

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.
  • 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 Sep 19 '17 at 16:01
  • Is there a way to return the plugin configuration as well? – Kevin Sep 19 '17 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? – Neograph734 Sep 19 '17 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 Sep 19 '17 at 16:45
  • 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 Sep 19 '17 at 19:23

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.