AjaxHelperTrait.php 853 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace Drupal\Core\Ajax;
  3. use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
  4. /**
  5. * Provides a helper to determine if the current request is via AJAX.
  6. *
  7. * @internal
  8. */
  9. trait AjaxHelperTrait {
  10. /**
  11. * Determines if the current request is via AJAX.
  12. *
  13. * @return bool
  14. * TRUE if the current request is via AJAX, FALSE otherwise.
  15. */
  16. protected function isAjax() {
  17. foreach (['drupal_ajax', 'drupal_modal', 'drupal_dialog'] as $wrapper) {
  18. if (strpos($this->getRequestWrapperFormat(), $wrapper) !== FALSE) {
  19. return TRUE;
  20. }
  21. }
  22. return FALSE;
  23. }
  24. /**
  25. * Gets the wrapper format of the current request.
  26. *
  27. * @string
  28. * The wrapper format.
  29. */
  30. protected function getRequestWrapperFormat() {
  31. return \Drupal::request()->get(MainContentViewSubscriber::WRAPPER_FORMAT);
  32. }
  33. }