You are here

class PluginInstanceConverter in Plugin 8.2

Converts plugin IDs in route parameters to plugin instances.

This is useful in case you want to create a plugin in the URI; for example, if you use plugins as entity bundles.

To use it, add a `plugin.plugin_instance` key to the route parameter's options. Its value is as follows:


example.route:
  path: foo/{bar}
  options:
    parameters:
      bar:
        plugin.plugin_instance:
          # Whether the conversion is enabled. Boolean. Optional. Defaults
          # to TRUE.
          enabled: TRUE
          # The ID of the instance's plugin type. String. Required.
          plugin_type_id: "foo.bar"

To use the default behavior, its value is as follows:


example.route:
  path: foo/{bar}d
  options:
    parameters:
      bar:
        plugin.plugin_type:
          # The ID of the instance's plugin type. String. Required.
          plugin_type_id: "foo.bar"

Hierarchy

Expanded class hierarchy of PluginInstanceConverter

1 file declares its use of PluginInstanceConverter
PluginInstanceConverterTest.php in tests/src/Unit/ParamConverter/PluginInstanceConverterTest.php
1 string reference to 'PluginInstanceConverter'
plugin.services.yml in ./plugin.services.yml
plugin.services.yml
1 service uses PluginInstanceConverter
plugin.paramconverter.plugin_instance in ./plugin.services.yml
Drupal\plugin\ParamConverter\PluginInstanceConverter

File

src/ParamConverter/PluginInstanceConverter.php, line 44

Namespace

Drupal\plugin\ParamConverter
View source
class PluginInstanceConverter implements ParamConverterInterface {
  use PluginTypeBasedConverterTrait;

  /**
   * {@inheritdoc}
   */
  public function doConvert($plugin_id, array $converter_definition) {
    $plugin_type = $this->pluginTypeManager
      ->getPluginType($converter_definition['plugin_type_id']);
    if ($plugin_type
      ->getPluginManager()
      ->hasDefinition($plugin_id)) {
      return $plugin_type
        ->getPluginManager()
        ->createInstance($plugin_id);
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  protected function getConverterDefinitionKey() {
    return 'plugin.plugin_instance';
  }

  /**
   * {@inheritdoc}
   */
  protected function getConverterDefinitionConstraint() {
    return new Collection([
      'enabled' => new Optional(new Type('boolean')),
      'plugin_type_id' => new Type('string'),
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PluginInstanceConverter::doConvert public function Converts path variables to their corresponding objects. Overrides PluginTypeBasedConverterTrait::doConvert
PluginInstanceConverter::getConverterDefinitionConstraint protected function Gets the parameter's converter definition validation constraint. Overrides PluginTypeBasedConverterTrait::getConverterDefinitionConstraint
PluginInstanceConverter::getConverterDefinitionKey protected function Gets the top-level route parameter definition key for this converter. Overrides PluginTypeBasedConverterTrait::getConverterDefinitionKey
PluginTypeBasedConverterTrait::$pluginTypeManager protected property The plugin type manager.
PluginTypeBasedConverterTrait::applies public function Implements \Drupal\Core\ParamConverter\ParamConverterInterface::applies().
PluginTypeBasedConverterTrait::convert public function Implements \Drupal\Core\ParamConverter\ParamConverterInterface::convert().
PluginTypeBasedConverterTrait::getConverterDefinition protected function Gets the converter-specific parameter definition.
PluginTypeBasedConverterTrait::validateParameterDefinition protected function Validates a route parameter's definition.
PluginTypeBasedConverterTrait::__construct public function Constructs a new instance.