I am facing an error in extracting audio files uploaded in zip format. The error is as follows- Argument 2 passed to Drupal\Core\Archiver\Zip::extract() must be of the type array, string given,
The following is the code:
public function buildForm(array $form, FormStateInterface $form_state, $node = NULL) {
// REMOVED PARTS OF CODE FOR READABILITY
if (isset($format) && $format == 'audio') {
$form['text_info']['fieldset']['source_formats']['audiofiles'] = [
'#type' => 'managed_file',
'#title' => $this->t('Upload zip file containing audios in mp3 format'),
'#upload_location' => 'public://file_uploads/audio/',
'#upload_validators' => $validators_zip,
];
}
}
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Import Content'),
];
return $form;
}
/**
* {@inheritdoc}
*/
/**
* Map content to the corresponding fields.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$params['text'] = $form_state->getValue('text');
$index = 0;
$params['source_id'] = $form_state->getValue('sources');
$params['format'] = $form_state->getValue('format');
$uploaded_audio_file_id = $form_state->getValue('audiofiles')[0];
$format = $form_state->getValue('format');
// REMOVED CODE FOR READABILITY
if ($format == 'audio') {
$path = File::load($uploaded_audio_file_id)->getFileUri();
Zip::extract("public://file_uploads/audio/extract", $path); // IT IS THROWING ERROR ON THIS LINE
$uploaded_files = [];
// Dir to scan.
$d = dir("/");
// Mind the strict bool check!
while (FALSE !== ($entry = $d->read())) {
if ($entry[0] == '.') {
// Ignore anything starting with a dot.
continue;
}
$uploaded_files[] = $entry;
}
$d->close();
// Or whatever desired.
sort($uploaded_files);
//print_r($uploaded_files);exit;
}
$batch = [
'title' => $this->t('processing...'),
'operations' => $operations,
'finished' => '\Drupal\heritage_bulk_upload\ImportContent::importContentFinishedCallback',
];
batch_set($batch);
}
}
Comments
This:
This:
...is returning a string. extract() expects an array. Change to this:
Hi,
Hi,
After making the changes, it now shows the error Error: Using $this when not in object context in Drupal\Core\Archiver\Zip::extract()
Please let me know what can I do about it?
That says to me you are
That says to me you are calling Zip() directly, when it's a service. You'll need to find out which service to load, and use that.
I did not come across a
I did not come across a service that deals with extracting the zip files...
There is a plugin manager for
There is a plugin manager for archivers: https://api.drupal.org/api/drupal/core%21core.services.yml/service/plugi...
You can likely use this plugin manager to retrieve the ZIP archiver, that will be loaded as a service.
Yes, I did across that
Yes, I did come across that service but I did not find enough resources to figure out how to extract the zip files. I am very new in Drupal and your help is greatly appreciated :)
I did something like, but it is throwing error:
Maybe this can help:
Maybe this can help:
https://www.drupal.org/docs/8/api/plugin-api/creating-your-own-plugin-ma...
getInstance() expects the
getInstance() expects the local path of your archive file, that is:
And extract() expects the path where archive files will be extracted:
Here is the working example:
Thank you, will try this one
Thank you, will try this one out :)
Hi,
Hi,
it gives me the error, Call to a member function getFileUri() on string.
How can I fix this?
This will only give the File
This will only give the File ID:
You need to load the file directly after that.
I used dependency injection.
I used dependency injection. Now I am getting the error Class 'ZipArchive' not found in Drupal\Core\Archiver\Zip->__construct()
I have pasted the code here, I can't figure out where is the mistake. I have removed parts of code for readability purpose:
This will only give the File
@Jaypan,
If you are talk about my code, then the file is loaded in validateForm() function.
@Jaypan,
Removed duplication.