You are here

public function TransactionCreateDeriver::getDerivativeDefinitions in Transaction 8

Gets the definition of all derivatives of a base plugin.

Parameters

array $base_plugin_definition: The definition array of the base plugin.

Return value

array An array of full derivative definitions keyed on derivative id.

Overrides DeriverBase::getDerivativeDefinitions

See also

getDerivativeDefinition()

File

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

Class

TransactionCreateDeriver
Derives transaction create plugin definitions based on transaction types.

Namespace

Drupal\transaction\Plugin\RulesAction

Code

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;
}