PathElement.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Drupal\Core\Render\Element;
  3. use Drupal\Core\Form\FormStateInterface;
  4. /**
  5. * Provides a matched path render element.
  6. *
  7. * Provides a form element to enter a path which can be optionally validated and
  8. * stored as either a \Drupal\Core\Url value object or a array containing a
  9. * route name and route parameters pair.
  10. *
  11. * @FormElement("path")
  12. */
  13. class PathElement extends Textfield {
  14. /**
  15. * Do not convert the submitted value from the user-supplied path.
  16. */
  17. const CONVERT_NONE = 0;
  18. /**
  19. * Convert the submitted value into a route name and parameter pair.
  20. */
  21. const CONVERT_ROUTE = 1;
  22. /**
  23. * Convert the submitted value into a \Drupal\Core\Url value object.
  24. */
  25. const CONVERT_URL = 2;
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function getInfo() {
  30. $info = parent::getInfo();
  31. $class = get_class($this);
  32. $info['#validate_path'] = TRUE;
  33. $info['#convert_path'] = self::CONVERT_ROUTE;
  34. $info['#element_validate'] = [
  35. [$class, 'validateMatchedPath'],
  36. ];
  37. return $info;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
  43. return NULL;
  44. }
  45. /**
  46. * Form element validation handler for matched_path elements.
  47. *
  48. * Note that #maxlength is validated by _form_validate() already.
  49. *
  50. * This checks that the submitted value matches an active route.
  51. */
  52. public static function validateMatchedPath(&$element, FormStateInterface $form_state, &$complete_form) {
  53. if (!empty($element['#value']) && ($element['#validate_path'] || $element['#convert_path'] != self::CONVERT_NONE)) {
  54. /** @var \Drupal\Core\Url $url */
  55. if ($url = \Drupal::service('path.validator')->getUrlIfValid($element['#value'])) {
  56. if ($url->isExternal()) {
  57. $form_state->setError($element, t('You cannot use an external URL, please enter a relative path.'));
  58. return;
  59. }
  60. if ($element['#convert_path'] == self::CONVERT_NONE) {
  61. // Url is valid, no conversion required.
  62. return;
  63. }
  64. // We do the value conversion here whilst the Url object is in scope
  65. // after validation has occurred.
  66. if ($element['#convert_path'] == self::CONVERT_ROUTE) {
  67. $form_state->setValueForElement($element, [
  68. 'route_name' => $url->getRouteName(),
  69. 'route_parameters' => $url->getRouteParameters(),
  70. ]);
  71. return;
  72. }
  73. elseif ($element['#convert_path'] == self::CONVERT_URL) {
  74. $form_state->setValueForElement($element, $url);
  75. return;
  76. }
  77. }
  78. $form_state->setError($element, t('This path does not exist or you do not have permission to link to %path.', [
  79. '%path' => $element['#value'],
  80. ]));
  81. }
  82. }
  83. }