You are here

public static function PathWidget::validateFormElement in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php \Drupal\path\Plugin\Field\FieldWidget\PathWidget::validateFormElement()

Form element validation handler for URL alias form element.

Parameters

array $element: The form element.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

File

core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php, line 86
Contains \Drupal\path\Plugin\Field\FieldWidget\PathWidget.

Class

PathWidget
Plugin implementation of the 'path' widget.

Namespace

Drupal\path\Plugin\Field\FieldWidget

Code

public static function validateFormElement(array &$element, FormStateInterface $form_state) {

  // Trim the submitted value of whitespace and slashes.
  $alias = rtrim(trim($element['alias']['#value']), " \\/");
  if (!empty($alias)) {
    $form_state
      ->setValueForElement($element['alias'], $alias);

    // Validate that the submitted alias does not exist yet.
    $is_exists = \Drupal::service('path.alias_storage')
      ->aliasExists($alias, $element['langcode']['#value'], $element['source']['#value']);
    if ($is_exists) {
      $form_state
        ->setError($element, t('The alias is already in use.'));
    }
  }
  if ($alias && $alias[0] !== '/') {
    $form_state
      ->setError($element, t('The alias needs to start with a slash.'));
  }
}