Commit c8080055 authored by Rob Phillips's avatar Rob Phillips
Browse files

Issue #3323859 by robphillips: Validate path formatting.

parent 6564fc67
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\dynamic_path_aliases;

use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
@@ -130,13 +131,20 @@ class PathRewriteForm extends EntityForm {
    if (!$this->entity->getRoute($form_state->getValue('route_name'))) {
      $form_state->setError($configuration['route_name'], $this->t('Route does not exist.'));
    }
    $path = $form_state->getValue('path');
    $param = "{{$entity->id()}}";
    if (mb_strpos($form_state->getValue('path'), $param) === FALSE) {
    if (!preg_match('/^\//', $path)) {
      $form_state->setError($configuration['path'], $this->t('Path must start with a forward slash.'));
    }
    elseif (!UrlHelper::isValid(preg_replace("/$param/i", '', $path))) {
      $form_state->setError($configuration['path'], $this->t('Path includes non-valid characters.'));
    }
    elseif (mb_strpos($path, $param) === FALSE) {
      $form_state->setError($configuration['path'], $this->t('Missing the @param route parameter.', [
        '@param' => $param,
      ]));
    }
    elseif ($this->entity->getRouteByPath($form_state->getValue('path'))) {
    elseif ($this->entity->getRouteByPath($path)) {
      $form_state->setError($configuration['path'], $this->t('Path is in use by another route.'));
    }
  }