PathProcessorFront.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Drupal\Core\PathProcessor;
  3. use Drupal\Core\Config\ConfigFactoryInterface;
  4. use Drupal\Core\Render\BubbleableMetadata;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. /**
  8. * Processes the inbound path by resolving it to the front page if empty.
  9. *
  10. * @todo - remove ::processOutbound() when we remove UrlGenerator::fromPath().
  11. */
  12. class PathProcessorFront implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
  13. /**
  14. * A config factory for retrieving required config settings.
  15. *
  16. * @var \Drupal\Core\Config\ConfigFactoryInterface
  17. */
  18. protected $config;
  19. /**
  20. * Constructs a PathProcessorFront object.
  21. *
  22. * @param \Drupal\Core\Config\ConfigFactoryInterface $config
  23. * A config factory for retrieving the site front page configuration.
  24. */
  25. public function __construct(ConfigFactoryInterface $config) {
  26. $this->config = $config;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function processInbound($path, Request $request) {
  32. if ($path === '/') {
  33. $path = $this->config->get('system.site')->get('page.front');
  34. if (empty($path)) {
  35. // We have to return a valid path but / won't be routable and config
  36. // might be broken so stop execution.
  37. throw new NotFoundHttpException();
  38. }
  39. }
  40. return $path;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
  46. // The special path '<front>' links to the default front page.
  47. if ($path === '/<front>') {
  48. $path = '/';
  49. }
  50. return $path;
  51. }
  52. }