You are here

public function SettingsForm::validateForm in File (Field) Paths 8

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/Form/SettingsForm.php, line 89

Class

SettingsForm
Administration settings form for File (Field) Paths.

Namespace

Drupal\filefield_paths\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $scheme = $this->streamWrapperManager
    ->getScheme($values['temp_location']);
  if (!$scheme) {
    $form_state
      ->setErrorByName('temp_location', $this
      ->t('Invalid file location. You must include a file stream wrapper (e.g., public://).'));
    return FALSE;
  }
  if (!$this->streamWrapperManager
    ->isValidScheme($scheme)) {
    $form_state
      ->setErrorByName('temp_location', $this
      ->t('Invalid file stream wrapper.'));
    return FALSE;
  }
  if ((!is_dir($values['temp_location']) || !is_writable($values['temp_location'])) && !$this->fileSystem
    ->prepareDirectory($values['temp_location'], FileSystem::CREATE_DIRECTORY | FileSystem::MODIFY_PERMISSIONS)) {
    $form_state
      ->setErrorByName('temp_location', $this
      ->t('File location can not be created or is not writable.'));
    return FALSE;
  }
  return TRUE;
}