3 namespace Drupal\ctools\Plugin\Deriver;
6 use Drupal\Core\Plugin\Context\ContextDefinition;
7 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
8 use Drupal\Core\TypedData\DataDefinitionInterface;
9 use Drupal\field\FieldConfigInterface;
11 class TypedDataRelationshipDeriver extends TypedDataPropertyDeriverBase implements ContainerDeriverInterface {
16 protected function generateDerivativeDefinition($base_plugin_definition, $data_type_id, $data_type_definition, DataDefinitionInterface $base_definition, $property_name, DataDefinitionInterface $property_definition) {
17 $bundle_info = $base_definition->getConstraint('Bundle');
18 // Identify base definitions that appear on bundle-able entities.
19 if ($bundle_info && array_filter($bundle_info) && $base_definition->getConstraint('EntityType')) {
20 $base_data_type = 'entity:' . $base_definition->getConstraint('EntityType');
22 // Otherwise, just use the raw data type identifier.
24 $base_data_type = $data_type_id;
26 // If we've not processed this thing before.
27 if (!isset($this->derivatives[$base_data_type . ':' . $property_name])) {
28 $derivative = $base_plugin_definition;
30 $derivative['label'] = $this->t($this->label, [
31 '@property' => $property_definition->getLabel(),
32 '@base' => $data_type_definition['label'],
34 $derivative['data_type'] = $property_definition->getFieldStorageDefinition()->getPropertyDefinition($property_definition->getFieldStorageDefinition()->getMainPropertyName())->getDataType();
35 $derivative['property_name'] = $property_name;
36 $context_definition = new ContextDefinition($base_data_type, $this->typedDataManager->createDataDefinition($base_data_type));
37 // Add the constraints of the base definition to the context definition.
38 if ($base_definition->getConstraint('Bundle')) {
39 $context_definition->addConstraint('Bundle', $base_definition->getConstraint('Bundle'));
41 $derivative['context'] = [
42 'base' => $context_definition,
44 $derivative['property_name'] = $property_name;
46 $this->derivatives[$base_data_type . ':' . $property_name] = $derivative;
48 // Individual fields can be on multiple bundles.
49 elseif ($property_definition instanceof FieldConfigInterface) {
50 // We should only end up in here on entity bundles.
51 $derivative = $this->derivatives[$base_data_type . ':' . $property_name];
53 /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
54 $label = $derivative['label'];
55 list(,, $argument_name) = explode(':', $data_type_id);
56 $arguments = $label->getArguments();
57 $arguments['@'. $argument_name] = $data_type_definition['label'];
58 $string_args = $arguments;
59 array_shift($string_args);
60 $last = array_slice($string_args, -1);
61 // The slice doesn't remove, so do that now.
62 array_pop($string_args);
63 $string = count($string_args) >= 2 ? '@property from '. implode(', ', array_keys($string_args)) .' and '. array_keys($last)[0] : '@property from @base and '. array_keys($last)[0];
64 $this->derivatives[$base_data_type . ':' . $property_name]['label'] = $this->t($string, $arguments);
65 if ($base_definition->getConstraint('Bundle')) {
66 // Add bundle constraints
67 $context_definition = $derivative['context']['base'];
68 $bundles = $context_definition->getConstraint('Bundle') ?: [];
69 $bundles = array_merge($bundles, $base_definition->getConstraint('Bundle'));
70 $context_definition->addConstraint('Bundle', $bundles);