system_check_directory

system_check_directory($form_element, FormStateInterface $form_state)

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.

File

core/modules/system/system.module, line 861
Configuration system that lets administrators modify the workings of the site.

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');
  if (!is_dir($directory) && !drupal_mkdir($directory, NULL, TRUE)) {
    // If the directory does not exists and cannot be created.
    $form_state->setErrorByName($form_element['#parents'][0], t('The directory %directory does not exist and could not be created.', array('%directory' => $directory)));
    $logger->error('The directory %directory does not exist and could not be created.', array('%directory' => $directory));
  }

  if (is_dir($directory) && !is_writable($directory) && !drupal_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.', array('%directory' => $directory)));
    $logger->error('The directory %directory exists but is not writable and could not be made writable.', array('%directory' => $directory));
  }
  elseif (is_dir($directory)) {
    if ($form_element['#name'] == 'file_public_path') {
      // Create public .htaccess file.
      file_save_htaccess($directory, FALSE);
    }
    else {
      // Create private .htaccess file.
      file_save_htaccess($directory);
    }
  }

  return $form_element;
}
doc_Drupal
2016-10-29 09:45:54
Comments
Leave a Comment

Please login to continue.