/** * Implements #element_validate callback for self::fieldSettingsForm(). */ 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); } }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $line_items_data = []; /** @var \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface $line_item */ foreach (PaymentLineItemsInput::getLineItems($form['container']['line_item'], $form_state) as $line_item) { $line_items_data[] = array('plugin_id' => $line_item->getPluginId(), 'plugin_configuration' => $line_item->getConfiguration()); } \Drupal::state()->set('payment_test_line_item_form_element', $line_items_data); }