--- b/modules/cart/src/Form/AddToCartForm.php (date 1701778080325) +++ b/modules/cart/src/Form/AddToCartForm.php (date 1701791173011) @@ -321,11 +321,11 @@ } $settings = $form_state->get('settings'); - if ($settings['redirect'] ?? FALSE) { - $this->destination = $settings['destination'] ?? 'cart'; + if (empty($settings['redirect_destination'])) { + $this->destination = FALSE; } else { - $this->destination = FALSE; + $this->destination = $settings['redirect_destination'] ?? 'cart'; } return $this->destination; --- b/modules/product/config/schema/commerce_product.schema.yml (date 1701778372348) +++ b/modules/product/config/schema/commerce_product.schema.yml (date 1701786287400) @@ -117,10 +117,7 @@ combine: type: boolean label: 'Whether to attempt to combine order items containing the same product variation' - redirect: - type: boolean - label: 'Redirect the customer after an item is added to the cart.' - destination: + redirect_destination: type: string label: 'Destination' --- b/modules/product/src/Plugin/Field/FieldFormatter/AddToCartFormatter.php (date 1701771592371) +++ b/modules/product/src/Plugin/Field/FieldFormatter/AddToCartFormatter.php (date 1701791010596) @@ -42,8 +42,7 @@ public static function defaultSettings() { return [ 'combine' => TRUE, - 'redirect' => FALSE, - 'destination' => 'cart', + 'redirect_destination' => '', ] + parent::defaultSettings(); } @@ -62,22 +61,30 @@ $form['redirect'] = [ '#type' => 'checkbox', '#title' => $this->t('Redirect the customer after an item is added to the cart.'), - '#default_value' => $this->getSetting('redirect'), + '#default_value' => !empty($this->getSetting('redirect_destination')), ]; $destination_options = [ + '' => $this->t('- None -'), 'cart' => $this->t('Cart'), ]; - if (!$this->moduleHandler->moduleExists('commerce_checkout')) { + if ($this->moduleHandler->moduleExists('commerce_checkout')) { $destination_options['checkout'] = $this->t('Checkout'); } - $form['destination'] = [ + $form['redirect_destination'] = [ '#type' => 'select', '#title' => $this->t('Destination'), '#options' => $destination_options, - '#default_value' => $this->getSetting('destination'), + '#default_value' => $this->getSetting('redirect_destination'), + '#states' => [ + 'visible' => [ + ':input[name$="[settings][redirect]"]' => ['checked' => TRUE], + ], + ], ]; + $form['#element_validate'][] = [get_class($this), 'validateSettingsForm']; + return $form; } @@ -93,6 +100,13 @@ $summary[] = $this->t('Do not combine order items containing the same product variation.'); } + if (empty($this->getSetting('redirect_destination'))) { + $summary[] = $this->t('Do not redirect anywhere after adding item to cart'); + } + else { + $summary[] = $this->t('Redirect to @destination page after adding item to the cart', ['@destination' => $this->getSetting('redirect_destination')]); + } + return $summary; } @@ -129,8 +143,7 @@ $product->id(), $view_mode, $this->getSetting('combine'), - $this->getSetting('redirect'), - $this->getSetting('destination'), + $this->getSetting('redirect_destination'), $langcode, ], ], @@ -151,2 +164,21 @@ + /** + * Provides validation for field settings. + * + * This validation step clears the redirect destination when the redirect is + * unchecked as it does not save in the field settings. + * + * @param array $form + * An associative array containing the structure of the form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + */ + public static function validateSettingsForm(array $form, FormStateInterface $form_state): void { + $triggerred_parents = $form_state->getTriggeringElement()['#parents']; + $redirect_value = $form_state->getValue($form['redirect']['#parents']); + if (!(bool) $redirect_value && end($triggerred_parents) !== 'cancel_settings') { + $form_state->setValue($form['redirect_destination']['#parents'], ''); + } + } + } --- b/modules/cart/tests/src/Functional/AddToCartFormTest.php (date 1701777477161) +++ b/modules/cart/tests/src/Functional/AddToCartFormTest.php (date 1701791411602) @@ -317,8 +317,7 @@ $display_repository = \Drupal::service('entity_display.repository'); $display = $display_repository->getViewDisplay('commerce_product', $product->bundle(), 'default'); $component = $display->getComponent('variations'); - $component['settings']['redirect'] = TRUE; - $component['settings']['destination'] = 'cart'; + $component['settings']['redirect_destination'] = 'cart'; $display->setComponent('variations', $component)->save(); $this->drupalGet($product->toUrl()); --- b/modules/checkout/tests/src/Functional/CheckoutOrderTest.php (date 1701777781529) +++ b/modules/checkout/tests/src/Functional/CheckoutOrderTest.php (date 1701791411584) @@ -997,8 +997,7 @@ $display_repository = \Drupal::service('entity_display.repository'); $display = $display_repository->getViewDisplay('commerce_product', $this->product->bundle(), 'default'); $component = $display->getComponent('variations'); - $component['settings']['redirect'] = TRUE; - $component['settings']['destination'] = 'checkout'; + $component['settings']['redirect_destination'] = 'checkout'; $display->setComponent('variations', $component)->save(); $this->drupalGet($this->product->toUrl()); --- b/modules/product/src/ProductLazyBuilders.php (date 1701778372394) +++ b/modules/product/src/ProductLazyBuilders.php (date 1701786287566) @@ -59,9 +59,7 @@ * The view mode used to render the product. * @param bool $combine * TRUE to combine order items containing the same product variation. - * @param bool $redirect - * TRUE to make direct redirect after adding product to the cart. - * @param string $destination + * @param string $redirect_destination * The destination where user should be redirected. * @param string $langcode * The langcode for the language that should be used in form. @@ -69,7 +67,7 @@ * @return array * A renderable array containing the cart form. */ - public function addToCartForm($product_id, $view_mode, $combine, $redirect, $destination, $langcode) { + public function addToCartForm($product_id, $view_mode, $combine, $redirect_destination, $langcode) { /** @var \Drupal\commerce_order\OrderItemStorageInterface $order_item_storage */ $order_item_storage = $this->entityTypeManager->getStorage('commerce_order_item'); /** @var \Drupal\commerce_product\Entity\ProductInterface $product */ @@ -95,8 +93,7 @@ 'view_mode' => $view_mode, 'settings' => [ 'combine' => $combine, - 'redirect' => $redirect, - 'destination' => $destination, + 'redirect_destination' => $redirect_destination, ], ]);