You are here

public function DomainConfigUIController::ajaxOperation in Domain Access 8

Handles AJAX operations to add/remove configuration forms.

Parameters

string $route_name: The route from which the AJAX request was triggered.

string $op: The operation being performed, either 'enable' to enable the form, 'disable' to disable the domain form, or 'remove' to disable the form and remove its stored configurations.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse A redirect response to redirect back to the calling form. Supported by the UrlGeneratorTrait.

1 string reference to 'DomainConfigUIController::ajaxOperation'
domain_config_ui.routing.yml in domain_config_ui/domain_config_ui.routing.yml
domain_config_ui/domain_config_ui.routing.yml

File

domain_config_ui/src/Controller/DomainConfigUIController.php, line 33

Class

DomainConfigUIController
Controller routines for AJAX callbacks for domain actions.

Namespace

Drupal\domain_config_ui\Controller

Code

public function ajaxOperation($route_name, $op) {
  $success = FALSE;

  // Get the query string for the return URL.
  $query = \Drupal::requestStack()
    ->getCurrentRequest()
    ->getQueryString();
  $params = [];
  $parts = explode('&', $query);
  foreach ($parts as $part) {
    $element = explode('=', $part);
    if ($element[0] !== 'token') {
      $params[$element[0]] = $element[1];
    }
  }
  $url = Url::fromRoute($route_name, $params);

  // Get current module settings.
  $config = \Drupal::configFactory()
    ->getEditable('domain_config_ui.settings');
  $path_pages = $this
    ->standardizePaths($config
    ->get('path_pages'));
  $new_path = '/' . $url
    ->getInternalPath();
  if (!$url
    ->isExternal() && $url
    ->access()) {
    switch ($op) {
      case 'enable':

        // Check to see if we already registered this form.
        if (!($exists = \Drupal::service('path.matcher')
          ->matchPath($new_path, $path_pages))) {
          $this
            ->addPath($new_path);
          $message = $this
            ->t('Form added to domain configuration interface.');
          $success = TRUE;
        }
        break;
      case 'disable':
        if ($exists = \Drupal::service('path.matcher')
          ->matchPath($new_path, $path_pages)) {
          $this
            ->removePath($new_path);
          $message = $this
            ->t('Form removed from domain configuration interface.');
          $success = TRUE;
        }
        break;
    }
  }

  // Set a message.
  if ($success) {
    \Drupal::messenger()
      ->addMessage($message);
  }
  else {
    \Drupal::messenger()
      ->addError($this
      ->t('The operation failed.'));
  }

  // Return to the invoking page.
  return new RedirectResponse($url
    ->toString(), 302);
}