DomainController.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Drupal\domain\Controller;
  3. use Drupal\Core\Url;
  4. use Drupal\Core\StringTranslation\StringTranslationTrait;
  5. use Drupal\domain\DomainInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. /**
  8. * Controller routines for AJAX callbacks for domain actions.
  9. */
  10. class DomainController {
  11. use StringTranslationTrait;
  12. /**
  13. * Handles AJAX operations from the overview form.
  14. *
  15. * @param \Drupal\domain\DomainInterface $domain
  16. * A domain record object.
  17. * @param string|null $op
  18. * The operation being performed, either 'default' to make the domain record
  19. * the default, 'enable' to enable the domain record, or 'disable' to
  20. * disable the domain record.
  21. *
  22. * Note: The delete action is handled by the entity form system.
  23. *
  24. * @return \Symfony\Component\HttpFoundation\RedirectResponse
  25. * A redirect response to redirect back to the domain record list.
  26. * Supported by the UrlGeneratorTrait.
  27. *
  28. * @see \Drupal\domain\DomainListBuilder
  29. */
  30. public function ajaxOperation(DomainInterface $domain, $op = NULL) {
  31. $success = FALSE;
  32. switch ($op) {
  33. case 'default':
  34. $domain->saveDefault();
  35. $message = $this->t('Domain record set as default');
  36. if ($domain->isDefault()) {
  37. $success = TRUE;
  38. }
  39. break;
  40. case 'enable':
  41. $domain->enable();
  42. $message = $this->t('Domain record has been enabled.');
  43. if ($domain->status()) {
  44. $success = TRUE;
  45. }
  46. break;
  47. case 'disable':
  48. $domain->disable();
  49. $message = $this->t('Domain record has been disabled.');
  50. if (!$domain->status()) {
  51. $success = TRUE;
  52. }
  53. break;
  54. }
  55. // Set a message.
  56. if ($success) {
  57. \Drupal::messenger()->addMessage($message);
  58. }
  59. else {
  60. \Drupal::messenger()->addMessage($this->t('The operation failed.'));
  61. }
  62. // Return to the invoking page.
  63. $url = Url::fromRoute('domain.admin', [], ['absolute' => TRUE]);
  64. return new RedirectResponse($url->toString(), 302);
  65. }
  66. }