You are here

function filelog_logging_settings_validate in File Log 8

Same name and namespace in other branches
  1. 2.0.x filelog.module \filelog_logging_settings_validate()

Form validation handler for system_logging_settings().

See also

filelog_form_system_logging_settings_alter()

1 string reference to 'filelog_logging_settings_validate'
filelog_form_system_logging_settings_alter in ./filelog.module
Implements hook_form_FORM_ID_alter() for system_logging_settings().

File

./filelog.module, line 231
Contains filelog.module.

Code

function filelog_logging_settings_validate(array $form, FormStateInterface $formState) {

  /** @var \Drupal\Core\File\FileSystemInterface $fileSystem */
  $fileSystem = Drupal::service('file_system');
  $streamWrapperManager = Drupal::service('stream_wrapper_manager');

  // Ignore the settings if logging is disabled.
  if ($formState
    ->getValue('filelog_enabled')) {

    // Place relative paths into the public files directory.
    $location = (string) $formState
      ->getValue([
      'filelog',
      'location',
    ]);
    if (!$streamWrapperManager
      ->getScheme($location) && $location[0] !== '/') {
      $location = 'public://' . $location;
      $formState
        ->setValue([
        'filelog',
        'location',
      ], $location);
    }

    // Set up the logging directory.
    if (!$fileSystem
      ->prepareDirectory($location, $fileSystem::CREATE_DIRECTORY) || !FileSecurity::writeHtaccess($location)) {
      $formState
        ->setError($form['filelog']['location'], t('The directory %dir could not be created, or is not writable.', [
        '%dir' => $location,
      ]));
    }

    // Ensure that gzip is enabled.
    if ($formState
      ->getValue([
      'filelog',
      'rotation',
      'gzip',
    ]) && !extension_loaded('zlib')) {
      $formState
        ->setError($form['filelog']['rotation']['gzip'], t('The <em>zlib</em> extension is required for gzip compression.'));
    }
  }
}