Skip to content
Snippets Groups Projects
Commit 16e0ee21 authored by Soliman Harkas's avatar Soliman Harkas
Browse files

Issue #3283322 by SolimanHarkas: Allow users to download pot file and add some improvement

parent 0ee132d7
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,8 @@ use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\system\FileDownloadController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
/**
* Form for export options.
......@@ -49,6 +51,7 @@ class ExportForm extends FormBase {
$instance->languageCurrentLanguageContext = $container->get('language.current_language_context');
$instance->loggerChannelDefault = $container->get('logger.channel.default');
$instance->string = $container->get('plugin.manager.string');
$instance->fileDownloadController = FileDownloadController::create($container);
return $instance;
}
......@@ -69,7 +72,11 @@ class ExportForm extends FormBase {
'#header' => [],
'#options' => [],
'#title' => $this->t('Table'),
'#weight' => '0',
];
$form['show_export'] = [
'#type' => 'checkbox',
'#default_value' => 0,
'#title' => $this->t('Export Pot File to machine path?'),
];
$form['destination'] = [
'#type' => 'textfield',
......@@ -78,12 +85,26 @@ class ExportForm extends FormBase {
'#maxlength' => 255,
'#required' => TRUE,
'#description' => t('A local file system path where "POT" file will be stored.'),
'#weight' => 1,
'#states' => [
'visible' => [
':input[name="show_export"]' => ['checked' => TRUE],
],
],
];
$form['actions']['download'] = [
'#type' => 'submit',
'#value' => $this->t('Download'),
'#submit' => [[$this, 'submitDownloadForm']],
];
$form['export'] = [
$form['actions']['export'] = [
'#type' => 'submit',
'#value' => $this->t('Export'),
'#weight' => 2,
'#submit' => [[$this, 'submitExportForm']],
'#states' => [
'visible' => [
':input[name="show_export"]' => ['checked' => TRUE],
],
],
];
return $form;
......@@ -103,6 +124,44 @@ class ExportForm extends FormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitExportForm(array &$form, FormStateInterface $form_state) {
$destination = $form_state->getValue('destination');
$output = $this->buildPotFile();
\Drupal::configFactory()->getEditable('string.settings')->set('destination.path', $destination)->save();
if (file_exists($destination)) {
$this->exportFile(implode("\n", $output), $destination);
}
else {
$this->messenger()->addError($this->t('Invalid stream wrapper: %destination', ['%destination' => $destination]));
}
}
/**
* {@inheritdoc}
*/
public function submitDownloadForm(array &$form, FormStateInterface $form_state) {
$destination = $form_state->getValue('destination');
if (file_exists($destination)) {
$this->downloadFile($destination, $form_state);
}
else {
$this->messenger()->addError($this->t('The file %destination does not exist', ['%destination' => $destination]));
}
}
/**
* Build pot file.
*
* @return string[]
* Build the Pot file to have necessary items and be more user friendly.
*/
protected function buildPotFile() {
$output = $this->getHeader();
if ($definitions = $this->string->getDefinitions()) {
foreach ($definitions as $item) {
......@@ -134,10 +193,7 @@ class ExportForm extends FormBase {
}
}
}
$destination = $form_state->getValue('destination');
\Drupal::configFactory()->getEditable('string.settings')->set('destination.path', $destination)->save();
$this->saveFile(implode("\n", $output), $destination);
return $output;
}
/**
......@@ -169,7 +225,7 @@ class ExportForm extends FormBase {
}
/**
* Save content to file.
* Export content to file.
*
* @param string $content
* Content of file to be saved.
......@@ -177,15 +233,34 @@ class ExportForm extends FormBase {
* Destination of file to be saved.
*
* @return \Drupal\file\FileInterface|false|\PHPUnit\Framework\MockObject\MockObject
* File that has been saved.
* File that has been exported.
*/
protected function saveFile(string $content, $destination) {
$file = file_save_data(
$content,
$destination,
FileSystemInterface::EXISTS_REPLACE
);
protected function exportFile(string $content, $destination) {
$file_repository = \Drupal::service('file.repository');
$file = $file_repository->writeData($content, $destination, FileSystemInterface::EXISTS_REPLACE);
$this->messenger()->addStatus($this->t('The translate items has been export to %destination', [
'%destination' => $destination,
]));
return $file;
}
/**
* Download file.
*
* @param string $destination
* Destination of file to be saved.
* @param object $form_state
* Values from submit method.
*
* @return \Drupal\file\FileInterface|false|\PHPUnit\Framework\MockObject\MockObject
* File that has been saved.
*/
protected function downloadFile($destination, $form_state) {
$response = new BinaryFileResponse($destination);
$response->setContentDisposition('attachment', 'string.pot');
$download = $form_state->setResponse($response);
return $download;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment