class EntityCreateDeriver in Rules 8.3
Derives entity create plugin definitions based on content entity types.
Hierarchy
- class \Drupal\Component\Plugin\Derivative\DeriverBase implements DeriverInterface
- class \Drupal\rules\Plugin\RulesAction\EntityCreateDeriver implements ContainerDeriverInterface uses StringTranslationTrait
Expanded class hierarchy of EntityCreateDeriver
See also
\Drupal\rules\Plugin\RulesAction\EntityCreate
File
- src/
Plugin/ RulesAction/ EntityCreateDeriver.php, line 21
Namespace
Drupal\rules\Plugin\RulesActionView source
class EntityCreateDeriver 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;
/**
* Creates a new EntityCreateDeriver object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field 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('Create a new @entity_type', [
'@entity_type' => $entity_type
->getSingularLabel(),
]),
'category' => $entity_type
->getLabel(),
'entity_type_id' => $entity_type_id,
'context_defintions' => [],
'provides' => [
$entity_type
->id() . '_created' => ContextDefinition::create("entity:{$entity_type_id}")
->setLabel($this
->t('Created @entity_type', [
'@entity_type' => $entity_type
->getSingularLabel(),
]))
->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');
$this->derivatives[$entity_type_id]['bundle_key'] = $bundle_key;
$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;
}
$item_definition = $definition
->getItemDefinition();
// @todo Add support for multi-property fields for entity creation.
// @see https://www.drupal.org/node/2748791.
$main_property_name = $item_definition
->getMainPropertyName();
if (is_null($main_property_name)) {
continue;
}
$type_definition = $item_definition
->getPropertyDefinition($main_property_name);
// Get around types which don't properly define their main property
// or lack a main property entirely.
if (is_null($type_definition)) {
continue;
}
// If this is an entity reference then we expect the target type as
// context.
if ($type_definition instanceof DataReferenceDefinitionInterface) {
$type_definition
->getTargetDefinition();
}
$type = $type_definition
->getDataType();
$is_bundle = $field_name == $bundle_key;
$multiple = $definition
->getCardinality() === 1 ? FALSE : TRUE;
$context_definition = ContextDefinition::create($type)
->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_definitions'][$field_name] = $context_definition;
}
}
return $this->derivatives;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DeriverBase:: |
protected | property | List of derivative definitions. | 1 |
DeriverBase:: |
public | function |
Gets the definition of a derivative plugin. Overrides DeriverInterface:: |
|
EntityCreateDeriver:: |
protected | property | The entity field manager. | |
EntityCreateDeriver:: |
protected | property | The entity type manager. | |
EntityCreateDeriver:: |
public static | function |
Creates a new class instance. Overrides ContainerDeriverInterface:: |
|
EntityCreateDeriver:: |
public | function |
Gets the definition of all derivatives of a base plugin. Overrides DeriverBase:: |
|
EntityCreateDeriver:: |
public | function | Creates a new EntityCreateDeriver object. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |