TransactionCreateDeriver.php in Transaction 8
File
src/Plugin/RulesAction/TransactionCreateDeriver.php
View source
<?php
namespace Drupal\transaction\Plugin\RulesAction;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
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;
use Drupal\transaction\TransactorPluginManagerInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\rules\Context\ContextDefinitionInterface;
class TransactionCreateDeriver extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $transactorManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, TransactorPluginManagerInterface $transactor_manager, TranslationInterface $string_translation) {
$this->entityTypeManager = $entity_type_manager;
$this->transactorManager = $transactor_manager;
$this->stringTranslation = $string_translation;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container
->get('entity_type.manager'), $container
->get('plugin.manager.transaction.transactor'), $container
->get('string_translation'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
foreach ($this->transactorManager
->getTransactors() as $transactor_id => $transactor_info) {
$contexts = [];
$contexts['transaction_type_id'] = ContextDefinition::create('string')
->setLabel($this
->t('Transaction type'))
->setRequired(TRUE)
->setAssignmentRestriction(ContextDefinitionInterface::ASSIGNMENT_RESTRICTION_INPUT)
->setDescription($this
->t('The transaction type ID for the new transaction.'));
$contexts['target_entity'] = ContextDefinition::create('entity')
->setLabel($this
->t('Target entity'))
->setRequired(TRUE)
->setAssignmentRestriction(ContextDefinitionInterface::ASSIGNMENT_RESTRICTION_SELECTOR)
->setDescription($this
->t('The target entity for the new transaction.'));
$contexts['operation_id'] = ContextDefinition::create('string')
->setLabel($this
->t('Operation'))
->setRequired(FALSE)
->setAssignmentRestriction(ContextDefinitionInterface::ASSIGNMENT_RESTRICTION_INPUT)
->setDescription($this
->t('An optional transaction operation.'));
foreach ([
'transaction',
'target',
] as $field_group) {
foreach ($transactor_info[$field_group . '_fields'] as $field_info) {
$field_definition = BaseFieldDefinition::create($field_info['type']);
$field_data_type = $field_definition
->getPropertyDefinition($field_definition
->getMainPropertyName())
->getDataType();
$contexts[$field_group . '_field_' . $field_info['name']] = ContextDefinition::create($field_data_type)
->setLabel($field_info['title'])
->setRequired($field_info['required'])
->setDescription($field_info['description']);
}
}
$this->derivatives[$transactor_id] = [
'label' => $this
->t('Create a new @transactor_type transaction', [
'@transactor_type' => $transactor_info['title'],
]),
'category' => $this
->t('Transaction'),
'context' => $contexts,
'provides' => [
'transaction' => ContextDefinition::create('entity:transaction')
->setLabel($this
->t('Transaction'))
->setRequired(TRUE),
],
] + $base_plugin_definition;
}
return $this->derivatives;
}
}