EntityCreate.php in Rules 8.3
File
src/Plugin/RulesAction/EntityCreate.php
View source
<?php
namespace Drupal\rules\Plugin\RulesAction;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\rules\Core\RulesActionBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityCreate extends RulesActionBase implements ContainerFactoryPluginInterface {
protected $storage;
protected $entityTypeId;
protected $bundleKey;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->storage = $storage;
$this->entityTypeId = $plugin_definition['entity_type_id'];
$this->bundleKey = $plugin_definition['bundle_key'];
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager')
->getStorage($plugin_definition['entity_type_id']));
}
public function refineContextDefinitions(array $selected_data) {
if ($type = $this->entityTypeId) {
$data_type = "entity:{$type}";
if ($this->bundleKey && ($bundle = $this
->getContextValue($this->bundleKey))) {
$data_type .= ":{$bundle}";
}
$this->pluginDefinition['provides'][$type . '_created']
->setDataType($data_type);
}
}
public function execute() {
$values = $this
->getContextValues();
$entity = $this->storage
->create($values);
$this
->setProvidedValue($this->entityTypeId . '_created', $entity);
}
}