You are here

class TransactionCreateDeriver in Transaction 8

Derives transaction create plugin definitions based on transaction types.

Hierarchy

Expanded class hierarchy of TransactionCreateDeriver

See also

\Drupal\transaction\Plugin\RulesAction\TransactionCreate

File

src/Plugin/RulesAction/TransactionCreateDeriver.php, line 21

Namespace

Drupal\transaction\Plugin\RulesAction
View source
class TransactionCreateDeriver extends DeriverBase implements ContainerDeriverInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The transactor plugin manager.
   *
   * @var \Drupal\transaction\TransactorPluginManager
   */
  protected $transactorManager;

  /**
   * Creates a new TransactionCreateDeriver object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\transaction\TransactorPluginManagerInterface $transactor_manager
   *   The transactor plugin manager.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    foreach ($this->transactorManager
      ->getTransactors() as $transactor_id => $transactor_info) {
      $contexts = [];

      // Transaction type context.
      $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.'));

      // Target entity context.
      $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.'));

      // Transaction type context.
      $contexts['operation_id'] = ContextDefinition::create('string')
        ->setLabel($this
        ->t('Operation'))
        ->setRequired(FALSE)
        ->setAssignmentRestriction(ContextDefinitionInterface::ASSIGNMENT_RESTRICTION_INPUT)
        ->setDescription($this
        ->t('An optional transaction operation.'));

      // Add transactor fields.
      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']);
        }
      }

      // Add the derivative.
      $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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TransactionCreateDeriver::$entityTypeManager protected property The entity type manager.
TransactionCreateDeriver::$transactorManager protected property The transactor plugin manager.
TransactionCreateDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
TransactionCreateDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
TransactionCreateDeriver::__construct public function Creates a new TransactionCreateDeriver object.