FormEntityTypedDataReferencedEntityDeriver.php in Flexiform 8
File
src/Plugin/Deriver/FormEntityTypedDataReferencedEntityDeriver.php
View source
<?php
namespace Drupal\flexiform\Plugin\Deriver;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\TypedDataManagerInterface;
use Drupal\ctools\Plugin\Deriver\TypedDataPropertyDeriverBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FormEntityTypedDataReferencedEntityDeriver extends TypedDataPropertyDeriverBase {
protected $entityBundleInfo;
public function __construct(TypedDataManagerInterface $typed_data_manager, TranslationInterface $string_translation, EntityTypeBundleInfoInterface $entity_bundle_info) {
parent::__construct($typed_data_manager, $string_translation);
$this->entityBundleInfo = $entity_bundle_info;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container
->get('typed_data_manager'), $container
->get('string_translation'), $container
->get('entity_type.bundle.info'));
}
protected function generateDerivativeDefinition($base_plugin_definition, $data_type_id, $data_type_definition, DataDefinitionInterface $base_definition, $property_name, DataDefinitionInterface $property_definition) {
if (!method_exists($property_definition, 'getType') || $property_definition
->getType() != 'entity_reference') {
return;
}
$bundle_info = $base_definition
->getConstraint('Bundle');
if ($bundle_info && array_filter($bundle_info) && $base_definition
->getConstraint('EntityType')) {
$base_data_type = 'entity:' . $base_definition
->getConstraint('EntityType');
}
else {
$base_data_type = $data_type_id;
}
if (!isset($this->derivatives[$base_data_type . ':' . $property_name])) {
$derivative = $base_plugin_definition;
$derivative['label'] = $this
->t('@property Entity from @base', [
'@property' => $property_definition
->getLabel(),
'@base' => $data_type_definition['label'],
]);
$context_definition = new ContextDefinition($base_data_type, $data_type_definition['label'], TRUE);
if ($base_definition
->getConstraint('Bundle')) {
$context_definition
->addConstraint('Bundle', $base_definition
->getConstraint('Bundle'));
}
$derivative['context'] = [
'base' => $context_definition,
];
$derivative['property_name'] = $property_name;
$derivative['entity_type'] = $property_definition
->getFieldStorageDefinition()
->getPropertyDefinition('entity')
->getConstraint('EntityType');
if ($bundle = $property_definition
->getFieldStorageDefinition()
->getPropertyDefinition('entity')
->getConstraint('Bundle')) {
$derivative['bundle'] = $bundle;
$this->derivatives[$base_data_type . ':' . $property_name . ':' . $bundle] = $derivative;
}
else {
$label = $derivative['label'];
foreach ($this->entityBundleInfo
->getBundleInfo($derivative['entity_type']) as $bundle => $info) {
$derivative['label'] = $label . ' (' . $info['label'] . ')';
$derivative['bundle'] = $bundle;
$this->derivatives[$base_data_type . ':' . $property_name . ':' . $bundle] = $derivative;
}
}
}
}
}