public function ShippingInformation::validatePaneForm in Commerce Shipping 8.2
Validates the pane form.
Parameters
array $pane_form: The pane form.
\Drupal\Core\Form\FormStateInterface $form_state: The form state of the parent form.
array $complete_form: The complete form structure.
Overrides CheckoutPaneBase::validatePaneForm
File
- src/
Plugin/ Commerce/ CheckoutPane/ ShippingInformation.php, line 383
Class
- ShippingInformation
- Provides the shipping information pane.
Namespace
Drupal\commerce_shipping\Plugin\Commerce\CheckoutPaneCode
public function validatePaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
$shipment_indexes = Element::children($pane_form['shipments']);
$triggering_element = $form_state
->getTriggeringElement();
$recalculate = !empty($triggering_element['#recalculate']);
$button_type = isset($triggering_element['#button_type']) ? $triggering_element['#button_type'] : '';
/** @var \Drupal\commerce\Plugin\Commerce\InlineForm\EntityInlineFormInterface $inline_form */
$inline_form = $pane_form['shipping_profile']['#inline_form'];
/** @var \Drupal\profile\Entity\ProfileInterface $profile */
$profile = $inline_form
->getEntity();
// The profile in form state needs to reflect the submitted values,
// for packers invoked on form rebuild, or "Billing same as shipping".
$form_state
->set('shipping_profile', $profile);
$shipments = [];
foreach ($shipment_indexes as $index) {
if (!isset($pane_form['shipments'][$index]['#shipment'])) {
continue;
}
$shipment = clone $pane_form['shipments'][$index]['#shipment'];
$form_display = EntityFormDisplay::collectRenderDisplay($shipment, 'checkout');
$form_display
->removeComponent('shipping_profile');
$form_display
->extractFormValues($shipment, $pane_form['shipments'][$index], $form_state);
$form_display
->validateFormValues($shipment, $pane_form['shipments'][$index], $form_state);
$shipment
->setShippingProfile($profile);
$shipments[] = $shipment;
}
if (!$recalculate && $button_type == 'primary' && !$shipments) {
// The checkout step was submitted without shipping being calculated.
// Force the recalculation now and reload the page.
$recalculate = TRUE;
$this
->messenger()
->addError($this
->t('Please select a shipping method.'));
$form_state
->setRebuild(TRUE);
}
$form_state
->set('recalculate_shipping', $recalculate);
// If another rate was selected, update the shipments and refresh the order
// to reflect the new rate in the order summary.
if (!empty($triggering_element['#rate'])) {
// Unfortunately, we have to save the shipment, otherwise
// $order->get('shipments')->referencedEntities() will return stale
// shipments in case the order is already referencing saved shipments.
array_map(function (ShipmentInterface $shipment) {
if (!$shipment
->isNew()) {
$shipment
->save();
}
}, $shipments);
$this->order
->set('shipments', $shipments);
$this->order
->save();
}
}