EntityCreateDeriver.php in Rules 8.3
File
src/Plugin/RulesAction/EntityCreateDeriver.php
View source
<?php
namespace Drupal\rules\Plugin\RulesAction;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\TypedData\DataReferenceDefinitionInterface;
use Drupal\rules\Context\ContextDefinition;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityCreateDeriver extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $entityFieldManager;
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;
}
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'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $entity_type) {
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;
$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();
$main_property_name = $item_definition
->getMainPropertyName();
if (is_null($main_property_name)) {
continue;
}
$type_definition = $item_definition
->getPropertyDefinition($main_property_name);
if (is_null($type_definition)) {
continue;
}
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;
}
}