You are here

public function PageGeneralForm::validatePath in Page Manager 8.4

Same name and namespace in other branches
  1. 8 page_manager_ui/src/Form/PageGeneralForm.php \Drupal\page_manager_ui\Form\PageGeneralForm::validatePath()

File

page_manager_ui/src/Form/PageGeneralForm.php, line 163

Class

PageGeneralForm

Namespace

Drupal\page_manager_ui\Form

Code

public function validatePath(&$element, FormStateInterface $form_state) {
  $cached_values = $form_state
    ->getTemporaryValue('wizard');

  /** @var $page \Drupal\page_manager\Entity\Page */
  $page = $cached_values['page'];

  // Ensure the path has a leading slash.
  if ($value = trim($element['#value'], '/')) {
    $value = '/' . $value;
    $form_state
      ->setValueForElement($element, $value);

    // Ensure each path is unique.
    $path_query = $this->pageStorage
      ->getQuery()
      ->condition('path', $value);
    if (!$page
      ->isNew()) {
      $path_query
        ->condition('id', $page
        ->id(), '<>');
    }
    $path = $path_query
      ->execute();
    if ($path) {
      $form_state
        ->setErrorByName('path', $this
        ->t('The page path must be unique.'));
    }
  }
  else {
    $form_state
      ->setErrorByName('path', $this
      ->t("Path is required."));
  }
}