TypedDataPropertyDeriverBase.php in Chaos Tool Suite (ctools) 8.3
File
src/Plugin/Deriver/TypedDataPropertyDeriverBase.php
View source
<?php
namespace Drupal\ctools\Plugin\Deriver;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\TypedData\ComplexDataInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\DataReferenceDefinitionInterface;
use Drupal\Core\TypedData\ListDataDefinitionInterface;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\field\Entity\FieldConfig;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class TypedDataPropertyDeriverBase extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
protected $typedDataManager;
protected $label = '@property from @base';
public function __construct(TypedDataManagerInterface $typed_data_manager, TranslationInterface $string_translation) {
$this->typedDataManager = $typed_data_manager;
$this->stringTranslation = $string_translation;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container
->get('typed_data_manager'), $container
->get('string_translation'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
foreach ($this->typedDataManager
->getDefinitions() as $data_type_id => $data_type_definition) {
if (is_subclass_of($data_type_definition['class'], ComplexDataInterface::class, TRUE)) {
$base_definition = $this->typedDataManager
->createDataDefinition($data_type_id);
foreach ($base_definition
->getPropertyDefinitions() as $property_name => $property_definition) {
if ($property_definition instanceof BaseFieldDefinition || $property_definition instanceof FieldConfig) {
$this
->generateDerivativeDefinition($base_plugin_definition, $data_type_id, $data_type_definition, $base_definition, $property_name, $property_definition);
}
}
}
}
return $this->derivatives;
}
protected function getDataType($property_definition) {
if ($property_definition instanceof DataReferenceDefinitionInterface) {
return $property_definition
->getTargetDefinition()
->getDataType();
}
if ($property_definition instanceof ListDataDefinitionInterface) {
return $property_definition
->getItemDefinition()
->getDataType();
}
return $property_definition
->getDataType();
}
protected abstract function generateDerivativeDefinition($base_plugin_definition, $data_type_id, $data_type_definition, DataDefinitionInterface $base_definition, $property_name, DataDefinitionInterface $property_definition);
}