You are here

public static function PaymentReference::fieldSettingsFormValidate in Payment 8.2

Implements #element_validate callback for self::fieldSettingsForm().

Overrides EntityReferenceItem::fieldSettingsFormValidate

File

modules/payment_reference/src/Plugin/Field/FieldType/PaymentReference.php, line 106

Class

PaymentReference
Provides a configurable payment reference field.

Namespace

Drupal\payment_reference\Plugin\Field\FieldType

Code

public static function fieldSettingsFormValidate(array $element, FormStateInterface $form_state) {
  $add_more_button_form_parents = array_merge($element['#array_parents'], [
    'line_items',
    'add_more',
    'add',
  ]);

  // Only set the field settings as a value when it is not the "Add more"
  // button that has been clicked.
  $triggering_element = $form_state
    ->getTriggeringElement();
  if ($triggering_element['#array_parents'] != $add_more_button_form_parents) {
    $values = $form_state
      ->getValues();
    $values = NestedArray::getValue($values, $element['#array_parents']);
    $line_items_data = [];
    foreach (PaymentLineItemsInput::getLineItems($element['line_items'], $form_state) as $line_item) {
      $line_items_data[] = [
        'plugin_id' => $line_item
          ->getPluginId(),
        'plugin_configuration' => $line_item
          ->getConfiguration(),
      ];
    }
    $value = [
      'currency_code' => $values['currency_code'],
      'line_items_data' => $line_items_data,
    ];
    $form_state
      ->setValueForElement($element, $value);
  }
}