View source
<?php
namespace Drupal\plugin\PluginType;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\plugin\PluginDefinition\PluginDefinitionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PluginType implements ConfigurablePluginTypeInterface {
use DependencySerializationTrait;
protected $configurationSchemaId = 'plugin.plugin_configuration.[plugin_type_id].[plugin_id]';
protected $container;
protected $id;
protected $fieldType = TRUE;
protected $label;
protected $description;
protected $operationsProvider;
protected $pluginDefinitionDecoratorClass;
protected $provider;
protected $pluginManagerServiceId;
public function __construct(array $definition, ContainerInterface $container, TranslationInterface $string_translation, ClassResolverInterface $class_resolver, TypedConfigManagerInterface $typed_config_manager) {
if (!is_string($definition['id']) || !strlen($definition['id'])) {
throw new \InvalidArgumentException(sprintf('The plugin type definition ID must be a non-empty string, but %s was given.', gettype($definition['id'])));
}
$this->id = $definition['id'];
$this->label = $definition['label'] = new TranslatableMarkup($definition['label'], [], [], $string_translation);
$this->description = $definition['description'] = isset($definition['description']) ? new TranslatableMarkup($definition['description'], [], [], $string_translation) : NULL;
if (array_key_exists('field_type', $definition)) {
if (!is_bool($definition['field_type'])) {
throw new \InvalidArgumentException(sprintf('The plugin type definition "field_type" item must be a boolean, but %s was given.', gettype($definition['field_type'])));
}
$this->fieldType = $definition['field_type'];
}
if (array_key_exists('plugin_definition_decorator_class', $definition)) {
$class = $definition['plugin_definition_decorator_class'];
if (!class_exists($class)) {
$type = is_scalar($class) ? $class : gettype($class);
throw new \InvalidArgumentException(sprintf('The plugin type definition "plugin_definition_decorator_class" item must valid class name, but "%s" was given and it does not exist.', $type));
}
$this->pluginDefinitionDecoratorClass = $definition['plugin_definition_decorator_class'];
}
if (isset($definition['plugin_configuration_schema_id'])) {
if (!is_string($definition['plugin_configuration_schema_id'])) {
throw new \InvalidArgumentException(sprintf('The plugin type definition "plugin_configuration_schema_id" item must be a string, but %s was given.', gettype($definition['field_type'])));
}
$this->configurationSchemaId = $definition['plugin_configuration_schema_id'];
}
$plugin_configuration_schema_id = $this
->getPluginConfigurationSchemaId('*');
if (!$typed_config_manager
->hasConfigSchema($plugin_configuration_schema_id)) {
throw new \InvalidArgumentException(sprintf('The plugin type definition "plugin_configuration_schema_id" item references the configuration schema "%s" ("%s"), which does not exist.', $plugin_configuration_schema_id, $this->configurationSchemaId));
}
$operations_provider_class = array_key_exists('operations_provider_class', $definition) ? $definition['operations_provider_class'] : DefaultPluginTypeOperationsProvider::class;
$this->operationsProvider = $class_resolver
->getInstanceFromDefinition($operations_provider_class);
$this->pluginManagerServiceId = $definition['plugin_manager_service_id'];
$this->provider = $definition['provider'];
$this->container = $container;
}
public static function createFromDefinition(ContainerInterface $container, array $definition) {
return new static($definition, $container, $container
->get('string_translation'), $container
->get('class_resolver'), $container
->get('config.typed'));
}
public function getId() {
return $this->id;
}
public function getLabel() {
return $this->label;
}
public function getDescription() {
return $this->description;
}
public function getProvider() {
return $this->provider;
}
public function getPluginManagerServiceName() {
return $this->pluginManagerServiceId;
}
public function getPluginManager() {
return $this->container
->get($this->pluginManagerServiceId);
}
public function ensureTypedPluginDefinition($plugin_definition) {
if ($this->pluginDefinitionDecoratorClass && !$plugin_definition instanceof $this->pluginDefinitionDecoratorClass) {
$plugin_definition_decorator_class = $this->pluginDefinitionDecoratorClass;
return $plugin_definition_decorator_class::createFromDecoratedDefinition($plugin_definition);
}
elseif ($plugin_definition instanceof PluginDefinitionInterface) {
return $plugin_definition;
}
else {
throw new \Exception(sprintf('A plugin definition of plugin type %s does not implement required %s, but its type also does not specify a plugin definition decorator.', $this
->getId(), PluginDefinitionInterface::class));
}
}
public function getOperationsProvider() {
return $this->operationsProvider;
}
public function isFieldType() {
return $this->fieldType;
}
public function getPluginConfigurationSchemaId($plugin_id) {
return str_replace([
'[plugin_type_id]',
'[plugin_id]',
], [
$this->id,
$plugin_id,
], $this->configurationSchemaId);
}
}