You are here

public function TransactorBase::submitConfigurationForm in Transaction 8

Handles the settings form submit for this transactor plugin.

Parameters

array $form: The form array.

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

Overrides PluginFormInterface::submitConfigurationForm

File

src/TransactorBase.php, line 467

Class

TransactorBase
Provides a base class for transactor plugins.

Namespace

Drupal\transaction

Code

public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\transaction\TransactionTypeInterface $transaction_type */
  if (!($transaction_type = $form_state
    ->getFormObject()
    ->getEntity())) {
    return;
  }

  // Process transactor fields.
  $values = $form_state
    ->getValues();
  foreach ($values as $key => $value) {
    if (empty($value) || !($field_info = $form_state
      ->getTemporaryValue('field_info_' . $key))) {

      // No value or not a field.
      continue;
    }
    $field_name = $value;

    // Create new fields.
    if ($value === '_create') {

      // New field, get the name from the given machine name in form.
      // Add prefix to the given field name.
      $field_name = $this->fieldPrefix . $values[$key . '_field_name'];
      $new_field = $this
        ->createFieldStorage($field_name, $field_info);
      $form_state
        ->setValue($key, $new_field
        ->getName());
    }

    // Add field to applicable bundles.
    $bundles = [];
    if ($field_info['entity_type'] == 'transaction') {

      // Transaction field.
      $bundles[] = $transaction_type
        ->id();
    }
    elseif ($field_info['entity_type'] == $transaction_type
      ->getTargetEntityTypeId()) {

      // Target entity field.
      $bundles = $transaction_type
        ->getBundles(TRUE);
    }
    foreach ($bundles as $bundle) {
      if (FieldConfig::loadByName($field_info['entity_type'], $bundle, $field_name)) {

        // Field already exists in bundle.
        continue;
      }
      $this
        ->createFieldConfig($field_name, $field_info, $bundle, $values[$key . '_label'], $values['id']);

      // Field display.
      if ($field_info['entity_type'] == 'transaction') {

        // Enable new field in the transaction form.
        $this
          ->setFieldDisplay($field_name, $field_info, $bundle, 'form');

        // Enable field display in the list view mode if set.
        if (!empty($field_info['list'])) {
          $this
            ->setFieldDisplay($field_name, $field_info, $bundle, 'view', 'list', [
            'label' => 'hidden',
          ]);
        }
      }

      // Enable field display in the default view mode.
      $this
        ->setFieldDisplay($field_name, $field_info, $bundle, 'view');
    }
  }

  // Save settings.
  $settings = $transaction_type
    ->getPluginSettings();
  foreach ([
    'transaction_fields',
    'target_fields',
    'options',
  ] as $group) {
    if (!isset($form[$group])) {
      continue;
    }
    foreach (Element::children($form[$group]) as $key) {
      if ($value = $form_state
        ->getValue($key)) {
        $settings[$key] = $value;
      }
      else {
        unset($settings[$key]);
      }
    }
  }
  $transaction_type
    ->setPluginSettings($settings);
}