You are here

public static function SwitchForm::switchCallback in Domain Access 8

Callback to remember save mode and reload page.

Parameters

array $form: The form array.

\Drupal\Core\Form\FormStateInterface $form_state: The form state array.

File

domain_config_ui/src/Form/SwitchForm.php, line 176

Class

SwitchForm
Class SwitchForm.

Namespace

Drupal\domain_config_ui\Form

Code

public static function switchCallback(array &$form, FormStateInterface $form_state) {

  // Extract requesting page URI from ajax URI.
  // Copied from Drupal\Core\Form\FormBuilder::buildFormAction().
  $request = \Drupal::service('request_stack')
    ->getMasterRequest();
  $request_uri = $request
    ->getRequestUri();

  // Prevent cross site requests via the Form API by using an absolute URL
  // when the request uri starts with multiple slashes.
  if (strpos($request_uri, '//') === 0) {
    $request_uri = $request
      ->getUri();
  }
  $parsed = UrlHelper::parse($request_uri);
  unset($parsed['query']['ajax_form'], $parsed['query'][MainContentViewSubscriber::WRAPPER_FORMAT]);
  if (\Drupal::config('domain_config_ui.settings')
    ->get('remember_domain')) {

    // Save domain and language on session.
    $_SESSION['domain_config_ui_domain'] = $form_state
      ->getValue('domain');
    $_SESSION['domain_config_ui_language'] = $form_state
      ->getValue('language');
  }
  else {

    // Pass domain and language as request query parameters.
    $parsed['query']['domain_config_ui_domain'] = $form_state
      ->getValue('domain');
    $parsed['query']['domain_config_ui_language'] = $form_state
      ->getValue('language');
  }
  $request_uri = $parsed['path'] . ($parsed['query'] ? '?' . UrlHelper::buildQuery($parsed['query']) : '');

  // Reload the page to get new form values.
  $response = new AjaxResponse();
  $response
    ->addCommand(new RedirectCommand($request_uri));
  return $response;
}