You are here

public function DomainController::ajaxOperation in Domain Access 8

Handles AJAX operations from the overview form.

Parameters

\Drupal\domain\DomainInterface $domain: A domain record object.

string|null $op: The operation being performed, either 'default' to make the domain record the default, 'enable' to enable the domain record, or 'disable' to disable the domain record.

Note: The delete action is handled by the entity form system.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse A redirect response to redirect back to the domain record list. Supported by the UrlGeneratorTrait.

See also

\Drupal\domain\DomainListBuilder

1 string reference to 'DomainController::ajaxOperation'
domain.routing.yml in domain/domain.routing.yml
domain/domain.routing.yml

File

domain/src/Controller/DomainController.php, line 35

Class

DomainController
Controller routines for AJAX callbacks for domain actions.

Namespace

Drupal\domain\Controller

Code

public function ajaxOperation(DomainInterface $domain, $op = NULL) {
  $success = FALSE;
  switch ($op) {
    case 'default':
      $domain
        ->saveDefault();
      $message = $this
        ->t('Domain record set as default');
      if ($domain
        ->isDefault()) {
        $success = TRUE;
      }
      break;
    case 'enable':
      $domain
        ->enable();
      $message = $this
        ->t('Domain record has been enabled.');
      if ($domain
        ->status()) {
        $success = TRUE;
      }
      break;
    case 'disable':
      $domain
        ->disable();
      $message = $this
        ->t('Domain record has been disabled.');
      if (!$domain
        ->status()) {
        $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.
  $url = Url::fromRoute('domain.admin', [], [
    'absolute' => TRUE,
  ]);
  return new RedirectResponse($url
    ->toString(), 302);
}