You are here

public function ConfigPagesTypeForm::validateForm in Config Pages 8

Same name and namespace in other branches
  1. 8.3 src/ConfigPagesTypeForm.php \Drupal\config_pages\ConfigPagesTypeForm::validateForm()
  2. 8.2 src/ConfigPagesTypeForm.php \Drupal\config_pages\ConfigPagesTypeForm::validateForm()

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

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

Overrides FormBase::validateForm

File

src/ConfigPagesTypeForm.php, line 139

Class

ConfigPagesTypeForm
Base form for category edit forms.

Namespace

Drupal\config_pages

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $new_menu_path = $form_state
    ->getValue('menu')['path'];
  if (!empty($new_menu_path) && !in_array($new_menu_path[0], [
    '/',
  ], TRUE)) {
    $form_state
      ->setErrorByName('menu', $this
      ->t('Manually entered paths should start with /'));
    return;
  }
  $old_menu_path = NULL;

  // Load unchanged entity.
  $config_pages_type = $this->entity;
  $config_pages_type_unchanged = $config_pages_type
    ->load($config_pages_type
    ->id());
  if (is_object($config_pages_type_unchanged)) {
    $old_menu_path = $config_pages_type_unchanged->menu['path'];
  }

  // If menu path was changed check if it's a valid Drupal path.
  if (!empty($new_menu_path) && $new_menu_path != $old_menu_path) {
    $path_exists = $this->pathValidator
      ->isValid($new_menu_path);
    if ($path_exists) {
      $form_state
        ->setErrorByName('menu', $this
        ->t('This menu path is already exists, please provide another one.'));
    }
    $this->routesRebuildRequired = TRUE;
  }
}