You are here

function pathauto_pattern_validate in Pathauto 8

Validate the pattern field, to ensure it doesn't contain any characters that are invalid in URLs.

1 string reference to 'pathauto_pattern_validate'
PatternEditForm::buildForm in src/Form/PatternEditForm.php
Form constructor.

File

./pathauto.module, line 150
pathauto Pathauto: Automatically generates aliases for content

Code

function pathauto_pattern_validate($element, FormStateInterface $form_state) {
  if (isset($element['#value'])) {
    $title = empty($element['#title']) ? $element['#parents'][0] : $element['#title'];
    $invalid_characters = [
      '#',
      '?',
      '&',
    ];
    $invalid_characters_used = [];
    foreach ($invalid_characters as $invalid_character) {
      if (strpos($element['#value'], $invalid_character) !== FALSE) {
        $invalid_characters_used[] = $invalid_character;
      }
    }
    if (!empty($invalid_characters_used)) {
      $form_state
        ->setError($element, t('The %element-title is using the following invalid characters: @invalid-characters.', [
        '%element-title' => $title,
        '@invalid-characters' => implode(', ', $invalid_characters_used),
      ]));
    }
    if (preg_match('/(\\s$)+/', $element['#value'])) {
      $form_state
        ->setError($element, t('The %element-title doesn\'t allow the patterns ending with whitespace.', [
        '%element-title' => $title,
      ]));
    }
  }
  return $element;
}