You are here

protected function TransactorBase::buildTargetFieldsForm in Transaction 8

Build configuration form fields to the target entity.

Parameters

array $form: The transaction type form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

Return value

array The form array.

1 call to TransactorBase::buildTargetFieldsForm()
TransactorBase::buildConfigurationForm in src/TransactorBase.php
Provides a form for this transactor plugin settings.

File

src/TransactorBase.php, line 214

Class

TransactorBase
Provides a base class for transactor plugins.

Namespace

Drupal\transaction

Code

protected function buildTargetFieldsForm(array $form, FormStateInterface $form_state) {
  if (!empty($this->pluginDefinition['target_entity_fields'])) {
    $form['target_fields'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Target entity fields'),
      '#description' => $this
        ->t('Fields in the target entity used by this type of transaction.'),
      '#open' => TRUE,
      '#tree' => FALSE,
      '#weight' => 20,
    ];

    /** @var \Drupal\transaction\TransactionTypeInterface $transaction_type */
    $transaction_type = $form_state
      ->getFormObject()
      ->getEntity();
    $transactor_settings = $transaction_type
      ->getPluginSettings();

    // Plugin field definitions.
    foreach ($this->pluginDefinition['target_entity_fields'] as $field) {

      // Set target entity type as the entity type in the field info.
      $field['entity_type'] = $transaction_type
        ->getTargetEntityTypeId();
      $field['settings'] = isset($field['settings']) ? $field['settings'] : [];

      // Entity reference fields in the target entity with no target type in
      // settings points to the transaction entity.
      if ($field['type'] == 'entity_reference' && !isset($field['settings']['target_type'])) {
        $field['settings']['target_type'] = 'transaction';
        $field['handler_settings']['target_bundles'] = [
          $transaction_type
            ->id(),
        ];
      }
      $form['target_fields'] += $this
        ->fieldReferenceSettingsFormField($field, isset($transactor_settings[$field['name']]) ? $transactor_settings[$field['name']] : NULL, $this
        ->getAvailableFields($field['entity_type'], $field['type'], [], $field['settings']));
      $form_state
        ->setTemporaryValue('field_info_' . $field['name'], $field);
    }
  }
  return $form;
}