function system_check_directory

Same name in other branches
  1. 7.x modules/system/system.module \system_check_directory()
  2. 9 core/modules/system/system.module \system_check_directory()
  3. 8.9.x core/modules/system/system.module \system_check_directory()
  4. 10 core/modules/system/system.module \system_check_directory()

Checks the existence of the directory specified in $form_element.

This function is called from the system_settings form to check all core file directories (file_public_path, file_private_path, file_temporary_path).

Parameters

$form_element: The form element containing the name of the directory to check.

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

1 string reference to 'system_check_directory'
LocaleHooks::formSystemFileSystemSettingsAlter in core/modules/locale/src/Hook/LocaleHooks.php
Implements hook_form_FORM_ID_alter() for system_file_system_settings().

File

core/modules/system/system.module, line 804

Code

function system_check_directory($form_element, FormStateInterface $form_state) {
    $directory = $form_element['#value'];
    if (strlen($directory) == 0) {
        return $form_element;
    }
    $logger = \Drupal::logger('file system');
    
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    if (!is_dir($directory) && !$file_system->mkdir($directory, NULL, TRUE)) {
        // If the directory does not exist and cannot be created.
        $form_state->setErrorByName($form_element['#parents'][0], t('The directory %directory does not exist and could not be created.', [
            '%directory' => $directory,
        ]));
        $logger->error('The directory %directory does not exist and could not be created.', [
            '%directory' => $directory,
        ]);
    }
    if (is_dir($directory) && !is_writable($directory) && !$file_system->chmod($directory)) {
        // If the directory is not writable and cannot be made so.
        $form_state->setErrorByName($form_element['#parents'][0], t('The directory %directory exists but is not writable and could not be made writable.', [
            '%directory' => $directory,
        ]));
        $logger->error('The directory %directory exists but is not writable and could not be made writable.', [
            '%directory' => $directory,
        ]);
    }
    elseif (is_dir($directory)) {
        if ($form_element['#name'] == 'file_public_path') {
            // Create public .htaccess file.
            FileSecurity::writeHtaccess($directory, FALSE);
        }
        else {
            // Create private .htaccess file.
            FileSecurity::writeHtaccess($directory);
        }
    }
    return $form_element;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.