/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { if (!$form_state->getRedirect()) { $data = \Drupal::moduleHandler()->invokeAll('uc_add_to_cart_data', array($form_state->getValues())); $msg = $this->config('uc_cart.settings')->get('add_item_msg'); $cart = Cart::create(\Drupal::getContainer()); $redirect = $cart->addItem($form_state->getValue('nid'), $form_state->getValue('qty'), $data, NULL, $msg); if (isset($redirect)) { $form_state->setRedirectUrl($redirect); } } }
/** * Submit handler for form buttons that do not complete a form workflow. * * The Edit View form is a multistep form workflow, but with state managed by * the SharedTempStore rather than $form_state->setRebuild(). Without this * submit handler, buttons that add or remove displays would redirect to the * destination parameter (e.g., when the Edit View form is linked to from a * contextual link). This handler can be added to buttons whose form submission * should not yet redirect to the destination. */ public function submitDelayDestination($form, FormStateInterface $form_state) { $request = $this->requestStack->getCurrentRequest(); $destination = $request->query->get('destination'); $redirect = $form_state->getRedirect(); // If there is a destination, and redirects are not explicitly disabled, add // the destination as a query string to the redirect and suppress it for the // current request. if (isset($destination) && $redirect !== FALSE) { // Create a valid redirect if one does not exist already. if (!$redirect instanceof Url) { $redirect = Url::createFromRequest($request); } // Add the current destination to the redirect unless one exists already. $options = $redirect->getOptions(); if (!isset($options['query']['destination'])) { $options['query']['destination'] = $destination; $redirect->setOptions($options); } $form_state->setRedirectUrl($redirect); $request->query->remove('destination'); } }
/** * {@inheritdoc} */ public function getRedirect() { return $this->mainFormState->getRedirect(); }
/** * {@inheritdoc} */ public function redirectForm(FormStateInterface $form_state) { $redirect = $form_state->getRedirect(); // Allow using redirect responses directly if needed. if ($redirect instanceof RedirectResponse) { return $redirect; } $url = NULL; // Check for a route-based redirection. if ($redirect instanceof Url) { $url = $redirect->setAbsolute()->toString(); } elseif ($redirect === NULL) { $request = $this->requestStack->getCurrentRequest(); $url = $this->urlGenerator->generateFromRoute('<current>', [], ['query' => $request->query->all(), 'absolute' => TRUE]); } if ($url) { // According to RFC 7231, 303 See Other status code must be used to redirect // user agent (and not default 302 Found). // @see http://tools.ietf.org/html/rfc7231#section-6.4.4 return new RedirectResponse($url, Response::HTTP_SEE_OTHER); } }
/** * {@inheritdoc} */ public function redirectForm(FormStateInterface $form_state) { $redirect = $form_state->getRedirect(); // Allow using redirect responses directly if needed. if ($redirect instanceof RedirectResponse) { return $redirect; } $url = NULL; // Check for a route-based redirection. if ($redirect instanceof Url) { $url = $redirect->setAbsolute()->toString(); } elseif ($redirect === NULL) { $request = $this->requestStack->getCurrentRequest(); // @todo Remove dependency on the internal _system_path attribute: // https://www.drupal.org/node/2293521. $url = $this->urlGenerator->generateFromPath($request->attributes->get('_system_path'), array('query' => $request->query->all(), 'absolute' => TRUE)); } if ($url) { // According to RFC 7231, 303 See Other status code must be used to redirect // user agent (and not default 302 Found). // @see http://tools.ietf.org/html/rfc7231#section-6.4.4 return new RedirectResponse($url, Response::HTTP_SEE_OTHER); } }
/** * {@inheritdoc} */ public function getRedirect() { return $this->decoratedFormState->getRedirect(); }
/** * @covers ::getRedirect * * @dataProvider providerGetRedirect * * @param bool $expected */ public function testGetRedirect($expected) { $this->decoratedFormState->getRedirect()->willReturn($expected)->shouldBeCalled(); $this->assertSame($expected, $this->formStateDecoratorBase->getRedirect()); }
/** * {@inheritdoc} */ public function redirectForm(FormStateInterface $form_state) { // According to RFC 7231, 303 See Other status code must be used to redirect // user agent (and not default 302 Found). // @see http://tools.ietf.org/html/rfc7231#section-6.4.4 $status_code = Response::HTTP_SEE_OTHER; $redirect = $form_state->getRedirect(); // Allow using redirect responses directly if needed. if ($redirect instanceof RedirectResponse) { return $redirect; } $url = NULL; // Check for a route-based redirection. if ($redirect instanceof Url) { $url = $redirect->toString(); } elseif (is_array($redirect)) { if (isset($redirect[1])) { $options = $redirect[1]; } else { $options = array(); } // Redirections should always use absolute URLs. $options['absolute'] = TRUE; if (isset($redirect[2])) { $status_code = $redirect[2]; } $url = $this->urlGenerator->generateFromPath($redirect[0], $options); } elseif (is_string($redirect)) { // This function can be called from the installer, which guarantees // that $redirect will always be a string, so catch that case here // and use the appropriate redirect function. if ($this->drupalInstallationAttempted()) { install_goto($redirect); } else { $url = $this->urlGenerator->generateFromPath($redirect, array('absolute' => TRUE)); } } elseif ($redirect === NULL) { $request = $this->requestStack->getCurrentRequest(); // @todo Remove dependency on the internal _system_path attribute: // https://www.drupal.org/node/2293521. $url = $this->urlGenerator->generateFromPath($request->attributes->get('_system_path'), array('query' => $request->query->all(), 'absolute' => TRUE)); } if ($url) { return new RedirectResponse($url, $status_code); } }