-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathEntitySaveDeriver.php
More file actions
111 lines (98 loc) · 4.01 KB
/
EntitySaveDeriver.php
File metadata and controls
111 lines (98 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* @file
* Contains \Drupal\rules\Plugin\RulesAction\EntitySaveDeriver.
*/
namespace Drupal\rules\Plugin\RulesAction;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\rules\Context\ContextDefinition;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Derives entity save plugin definitions based on content entity types.
*
* @see \Drupal\rules\Plugin\RulesAction\EntitySave
*/
class EntitySaveDeriver extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface;
*/
protected $entityFieldManager;
/**
* Saves a new EntitySaveDeriver object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, TranslationInterface $string_translation) {
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->stringTranslation = $string_translation;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container->get('entity_type.manager'), $container->get('entity_field.manager'), $container->get('string_translation'));
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
// Only allow content entities and ignore configuration entities.
if (!$entity_type instanceof ContentEntityTypeInterface) {
continue;
}
$this->derivatives[$entity_type_id] = [
'label' => $this->t('Save @entity_type', ['@entity_type' => $entity_type->getLowercaseLabel()]),
'category' => $entity_type->getLabel(),
'entity_type_id' => $entity_type_id,
'context' => [],
'provides' => [
'entity' => ContextDefinition::create("entity:$entity_type_id")
->setLabel($entity_type->getLabel())
->setRequired(TRUE),
],
] + $base_plugin_definition;
// Add a required context for the bundle key, and optional contexts for
// other required base fields. This matches the storage create() behavior,
// where only the bundle requirement is enforced.
$bundle_key = $entity_type->getKey('bundle');
$base_field_definitions = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id);
foreach ($base_field_definitions as $field_name => $definition) {
if ($field_name != $bundle_key && !$definition->isRequired()) {
continue;
}
$is_bundle = ($field_name == $bundle_key);
$multiple = ($definition->getCardinality() === 1) ? FALSE : TRUE;
$context_definition = ContextDefinition::create($definition->getType())
->setLabel($definition->getLabel())
->setRequired($is_bundle)
->setMultiple($multiple)
->setDescription($definition->getDescription());
if ($is_bundle) {
$context_definition->setAssignmentRestriction(ContextDefinition::ASSIGNMENT_RESTRICTION_INPUT);
}
$this->derivatives[$entity_type_id]['context'][$field_name] = $context_definition;
}
}
return $this->derivatives;
}
}