You are here

class PluginDefinitionConverter in Plugin 8.2

Converts plugin IDs in route parameters to plugin definitions.

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


example.route:
  path: foo/{bar}
  options:
    parameters:
      bar:
        plugin.plugin_definition:
          # Whether the conversion is enabled. Boolean. Optional. Defaults
          # to TRUE.
          enabled: TRUE
          # The ID of the definition'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_definition:
          # The ID of the definition's plugin type. String. Required.
          plugin_type_id: "foo.bar"

Hierarchy

Expanded class hierarchy of PluginDefinitionConverter

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

File

src/ParamConverter/PluginDefinitionConverter.php, line 41

Namespace

Drupal\plugin\ParamConverter
View source
class PluginDefinitionConverter 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)) {
      try {
        return $plugin_type
          ->ensureTypedPluginDefinition($plugin_type
          ->getPluginManager()
          ->getDefinition($plugin_id));
      } catch (\InvalidArgumentException $e) {

        // The only way to see if a typed definition can be ensured is by trying
        // to ensure it. An exception is therefore expected and should not
        // bubble up.
        return NULL;
      }
    }
    return NULL;
  }

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

  /**
   * {@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
PluginDefinitionConverter::doConvert public function Converts path variables to their corresponding objects. Overrides PluginTypeBasedConverterTrait::doConvert
PluginDefinitionConverter::getConverterDefinitionConstraint protected function Gets the parameter's converter definition validation constraint. Overrides PluginTypeBasedConverterTrait::getConverterDefinitionConstraint
PluginDefinitionConverter::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.