AdminContext.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Drupal\Core\Routing;
  3. use Symfony\Component\Routing\Route;
  4. /**
  5. * Provides a helper class to determine whether the route is an admin one.
  6. */
  7. class AdminContext {
  8. /**
  9. * The route match.
  10. *
  11. * @var \Drupal\Core\Routing\RouteMatchInterface
  12. */
  13. protected $routeMatch;
  14. /**
  15. * Construct a new admin context helper instance.
  16. *
  17. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
  18. * The route match.
  19. */
  20. public function __construct(RouteMatchInterface $route_match) {
  21. $this->routeMatch = $route_match;
  22. }
  23. /**
  24. * Determines whether the active route is an admin one.
  25. *
  26. * @param \Symfony\Component\Routing\Route $route
  27. * (optional) The route to determine whether it is an admin one. Per default
  28. * this falls back to the route object on the active request.
  29. *
  30. * @return bool
  31. * Returns TRUE if the route is an admin one, otherwise FALSE.
  32. */
  33. public function isAdminRoute(Route $route = NULL) {
  34. if (!$route) {
  35. $route = $this->routeMatch->getRouteObject();
  36. if (!$route) {
  37. return FALSE;
  38. }
  39. }
  40. return (bool) $route->getOption('_admin_route');
  41. }
  42. }