View source
<?php
namespace Drupal\transaction\Plugin\RulesAction;
use Drupal\rules\Core\RulesActionBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Psr\Log\LoggerInterface;
class TransactionCreate extends RulesActionBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $logger;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, LoggerInterface $logger) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->logger = $logger;
}
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'), $container
->get('logger.factory')
->get('rules'));
}
public function refineContextDefinitions(array $selected_data) {
if ($transaction_type_id = $this
->getContextValue('transaction_type_id')) {
$data_type = 'entity:transaction:' . $transaction_type_id;
$this->pluginDefinition['provides']['transaction']
->setDataType($data_type);
}
}
public function execute() {
$values = $this
->getContextValues();
if (!($transaction_type = $this->entityTypeManager
->getStorage('transaction_type')
->load($values['transaction_type_id']))) {
$this->logger
->error('Transaction type @type not found in action rule %rule', [
'@type' => $values['transaction_type_id'],
'%rule' => $this
->getLabelValue(),
]);
return;
}
if ($transaction_type
->getPluginId() != $this
->getDerivativeId()) {
$this->logger
->error('Mismatch transactor in rule %rule for transaction type %type', [
'%rule' => $this
->getLabelValue(),
'%type' => $transaction_type
->label(),
]);
return;
}
$target_entity = $values['target_entity'];
if (!$transaction_type
->isApplicable($target_entity)) {
$this->logger
->error('Transaction type %type not applicable to the target entity %target', [
'%type' => $transaction_type
->label(),
'%target' => $target_entity
->label(),
]);
return;
}
$transaction = $this->entityTypeManager
->getStorage('transaction')
->create([
'type' => $transaction_type
->id(),
'target_entity' => $target_entity,
]);
if (!empty($values['operation_id'])) {
$transaction
->setOperation($values['operation_id']);
}
$settings = $transaction_type
->getPluginSettings();
foreach ([
'transaction',
'target',
] as $field_group) {
$entity = $field_group == 'transaction' ? $transaction : $target_entity;
$field_prefix = $field_group . '_field_';
foreach ($values as $key => $value) {
if (strpos($key, $field_prefix) === 0) {
$transactor_field_name = substr($key, strlen($field_prefix));
if (!empty($settings[$transactor_field_name]) && $entity
->hasField($settings[$transactor_field_name])) {
$entity
->get($settings[$transactor_field_name])
->setValue($value);
}
}
}
}
$this
->setProvidedValue('transaction', $transaction);
}
}