PluginCollectionItemBase.php in Plugin 8.2
File
src/Plugin/Field/FieldType/PluginCollectionItemBase.php
View source
<?php
namespace Drupal\plugin\Plugin\Field\FieldType;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\MapDataDefinition;
use Drupal\plugin\PluginType\ConfigurablePluginTypeInterface;
abstract class PluginCollectionItemBase extends FieldItemBase implements PluginCollectionItemInterface {
public function __get($name) {
if ($this->definition
->getPropertyDefinition($name)) {
return $this
->get($name)
->getValue();
}
elseif (isset($this->values[$name])) {
return $this->values[$name];
}
}
public function validatePluginInstance(PluginInspectionInterface $plugin_instance) {
$plugin_manager = $this
->getPluginType()
->getPluginManager();
if (!$plugin_manager
->hasDefinition($plugin_instance
->getPluginId())) {
throw new PluginNotFoundException($plugin_instance
->getPluginId(), sprintf('Plugin manager %s does not have a definition for plugin %s.', get_class($plugin_manager), $plugin_instance
->getPluginId()));
}
}
public function createContainedPluginInstance($plugin_id, array $plugin_configuration = []) {
$plugin_instance = $this
->getPluginType()
->getPluginManager()
->createInstance($plugin_id, $plugin_configuration);
$this
->validatePluginInstance($plugin_instance);
return $plugin_instance;
}
public function getContainedPluginInstance() {
return $this
->get('plugin_instance')
->getValue();
}
public function setContainedPluginInstance(PluginInspectionInterface $plugin_instance) {
$this
->get('plugin_instance')
->setValue($plugin_instance);
return $this;
}
public function resetContainedPluginInstance() {
$this
->get('plugin_instance')
->applyDefaultValue();
return $this;
}
public function getContainedPluginId() {
return $this
->get('plugin_id')
->getValue();
}
public function setContainedPluginId($plugin_id) {
$this
->get('plugin_id')
->setValue($plugin_id);
return $this;
}
public function getContainedPluginConfiguration() {
return $this
->get('plugin_configuration')
->getValue();
}
public function setContainedPluginConfiguration(array $configuration) {
$this
->get('plugin_configuration')
->setValue($configuration);
return $this;
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['plugin_id'] = DataDefinition::create('plugin_id')
->setLabel(t('Plugin ID'));
$properties['plugin_configuration'] = MapDataDefinition::create('plugin_configuration')
->setLabel(t('Plugin configuration'));
$properties['plugin_instance'] = MapDataDefinition::create('plugin_instance')
->setLabel(t('Plugin instance'))
->setComputed(TRUE);
return $properties;
}
public static function mainPropertyName() {
return 'plugin_instance';
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$columns = [
'plugin_id' => [
'description' => 'The plugin ID.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
],
'plugin_configuration' => [
'description' => 'The plugin configuration.',
'type' => 'blob',
'not null' => TRUE,
'serialize' => TRUE,
],
];
$schema = [
'columns' => $columns,
];
return $schema;
}
public function isEmpty() {
return !$this
->getContainedPluginInstance();
}
public function getValue() {
$plugin_type = $this
->getPluginType();
return [
'plugin_id' => $this
->get('plugin_id')
->getValue(),
'plugin_configuration' => $this
->get('plugin_configuration')
->getValue(),
'plugin_configuration_schema_id' => $plugin_type instanceof ConfigurablePluginTypeInterface ? $plugin_type
->getPluginConfigurationSchemaId($this
->get('plugin_id')
->getValue()) : 'plugin.plugin_configuration.*.*',
];
}
public function setValue($values, $notify = TRUE) {
if ($values instanceof PluginInspectionInterface) {
$this
->setContainedPluginInstance($values);
}
elseif (is_array($values)) {
if (isset($values['plugin_instance'])) {
$this
->setContainedPluginInstance($values['plugin_instance']);
}
else {
if (isset($values['plugin_id'])) {
$this
->setContainedPluginId($values['plugin_id']);
}
if (isset($values['plugin_configuration'])) {
$this
->setContainedPluginConfiguration($values['plugin_configuration']);
}
}
}
elseif (!is_null($values)) {
$type = is_object($values) ? get_class($values) : gettype($values);
throw new \InvalidArgumentException(sprintf('The value must implement %s or be an associative array, but %s was given', PluginInspectionInterface::class, $type));
}
}
public function __isset($name) {
return parent::__isset($this
->mainPropertyName());
}
}