PathProcessorAlias.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Drupal\Core\PathProcessor;
  3. use Drupal\Core\Path\AliasManagerInterface;
  4. use Drupal\Core\Render\BubbleableMetadata;
  5. use Symfony\Component\HttpFoundation\Request;
  6. /**
  7. * Processes the inbound path using path alias lookups.
  8. *
  9. * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use
  10. * \Drupal\path_alias\PathProcessor\AliasPathProcessor.
  11. *
  12. * @see https://www.drupal.org/node/3092086
  13. */
  14. class PathProcessorAlias implements InboundPathProcessorInterface, OutboundPathProcessorInterface {
  15. /**
  16. * An alias manager for looking up the system path.
  17. *
  18. * @var \Drupal\Core\Path\AliasManagerInterface
  19. */
  20. protected $aliasManager;
  21. /**
  22. * Constructs a PathProcessorAlias object.
  23. *
  24. * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
  25. * An alias manager for looking up the system path.
  26. */
  27. public function __construct(AliasManagerInterface $alias_manager) {
  28. $this->aliasManager = $alias_manager;
  29. // This is used as base class by the new class, so we do not trigger
  30. // deprecation notices when that or any child class is instantiated.
  31. $new_class = 'Drupal\path_alias\PathProcessor\AliasPathProcessor';
  32. if (!is_a($this, $new_class) && class_exists($new_class)) {
  33. @trigger_error('The \\' . __CLASS__ . ' class is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Instead, use \\' . $new_class . '. See https://drupal.org/node/3092086', E_USER_DEPRECATED);
  34. }
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function processInbound($path, Request $request) {
  40. $path = $this->aliasManager->getPathByAlias($path);
  41. return $path;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
  47. if (empty($options['alias'])) {
  48. $langcode = isset($options['language']) ? $options['language']->getId() : NULL;
  49. $path = $this->aliasManager->getAliasByPath($path, $langcode);
  50. // Ensure the resulting path has at most one leading slash, to prevent it
  51. // becoming an external URL without a protocol like //example.com. This
  52. // is done in \Drupal\Core\Routing\UrlGenerator::generateFromRoute()
  53. // also, to protect against this problem in arbitrary path processors,
  54. // but it is duplicated here to protect any other URL generation code
  55. // that might call this method separately.
  56. if (strpos($path, '//') === 0) {
  57. $path = '/' . ltrim($path, '/');
  58. }
  59. }
  60. return $path;
  61. }
  62. }