Skip to content
Snippets Groups Projects

Issue #3511062 by mably: Add a new option to allow removal of existing files in the upload folder

Merged Issue #3511062 by mably: Add a new option to allow removal of existing files in the upload folder
Merged Frank Mably requested to merge issue/responsive_favicons-3511062:3511062-add-a-new into 3.x
@@ -112,6 +112,9 @@ class ResponsiveFaviconsAdmin extends ConfigFormBase {
'visible' => [
':input[name="path_type"]' => ['value' => 'upload'],
],
'required' => [
':input[name="path_type"]' => ['value' => 'upload'],
],
],
];
$form['local_path'] = [
@@ -143,6 +146,17 @@ class ResponsiveFaviconsAdmin extends ConfigFormBase {
],
],
];
$form['remove_existing_files'] = [
'#type' => 'checkbox',
'#title' => $this->t('Remove previously uploaded files'),
'#description' => $this->t('If checked, all existing files will be recursively deleted from the upload folder. Use with care.'),
'#default_value' => FALSE,
'#states' => [
'visible' => [
':input[name="files[upload]"]' => ['filled' => TRUE],
],
],
];
$form['cache_refresh_suffix'] = [
'#type' => 'checkbox',
'#title' => $this->t('Add a cache refresh suffix to icons URLs'),
@@ -231,12 +245,18 @@ class ResponsiveFaviconsAdmin extends ConfigFormBase {
$files = $archive->listContents();
// You cannot validate the form in the submit handler.
// Display a warning if the zip file is empty.
if (!$files) {
$this->messenger()->addError($this->t('The uploaded archive is empty.'));
return;
}
$destination = 'public://' . $path;
// Remove existing files if requested.
if ($form_state->getValue('remove_existing_files')) {
$this->fileSystem->deleteRecursive($destination);
}
// Recreated directory to extract the new files.
$this->fileSystem->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY);
// Copy the files to the correct location.
Loading