function FileExampleSubmitHandlerHelper::handleDirectoryCreate

Submit handler for directory creation.

Here we create a directory and set proper permissions on it using FileSystemInterface::prepareDirectory().

File

modules/file_example/src/FileExampleSubmitHandlerHelper.php, line 388

Class

FileExampleSubmitHandlerHelper
A submit handler helper class for the file_example module.

Namespace

Drupal\file_example

Code

public function handleDirectoryCreate(array &$form, FormStateInterface $form_state) {
    $form_values = $form_state->getValues();
    $directory = $form_values['directory_name'];
    // The options passed to FileSystemInterface::prepareDirectory() are a
    // bitmask, so we can specify
    // either FileSystemInterface::MODIFY_PERMISSIONS
    // (set permissions on the directory),
    // FileSystemInterface::CREATE_DIRECTORY,
    // or both together:
    // FileSystemInterface::MODIFY_PERMISSIONS |
    // FileSystemInterface::CREATE_DIRECTORY.
    // FileSystemInterface::MODIFY_PERMISSIONS
    // will set the permissions of the directory by default to 0755,
    // or to the value of the variable
    // 'file_chmod_directory'.
    if (!$this->fileSystem
        ->prepareDirectory($directory, FileSystemInterface::MODIFY_PERMISSIONS | FileSystemInterface::CREATE_DIRECTORY)) {
        $this->messenger
            ->addMessage($this->t('Failed to create %directory.', [
            '%directory' => $directory,
        ]), 'error');
    }
    else {
        $this->messenger
            ->addMessage($this->t('Directory %directory is ready for use.', [
            '%directory' => $directory,
        ]));
        $this->stateHelper
            ->setDefaultDirectory($directory);
    }
}