3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Provide a downloadable CSV template with column headers.

This commit is contained in:
Michael Stenta 2023-09-13 10:19:58 -04:00
parent fcdbf70baf
commit 298d5c8bc4
3 changed files with 52 additions and 2 deletions

View file

@ -16,3 +16,13 @@ farm.import.csv.form:
parameters:
migration_id:
type: string
farm.import.csv.template:
path: /import/csv/{migration_id}/template
defaults:
_controller: \Drupal\farm_import_csv\Form\CsvImportForm::template
requirements:
_custom_access: \Drupal\farm_import_csv\Form\CsvImportForm::access
options:
parameters:
migration_id:
type: string

View file

@ -5,8 +5,10 @@ namespace Drupal\farm_import_csv\Form;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Link;
use Drupal\Core\Session\AccountInterface;
use Drupal\migrate_source_ui\Form\MigrateSourceUiForm;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
/**
@ -95,7 +97,7 @@ class CsvImportForm extends MigrateSourceUiForm {
'#title' => $this->t('CSV Columns'),
];
// Show a description of the columns.
// Show a description of the columns with a link to download a template.
$items = [];
foreach ($migration['third_party_settings']['farm_import_csv']['columns'] as $info) {
if (!empty($info['name'])) {
@ -106,13 +108,48 @@ class CsvImportForm extends MigrateSourceUiForm {
$items[] = Markup::create($item);
}
}
$template_link = Link::createFromRoute($this->t('Download template'), 'farm.import.csv.template', ['migration_id' => $migration_id]);
$form['columns']['descriptions'] = [
'#theme' => 'item_list',
'#items' => $items,
'#suffix' => '<p>' . $template_link->toString() . '</p>',
];
}
return $form;
}
/**
* Download a template for a CSV migration.
*
* @param string $migration_id
* The migration ID.
*
* @return \Symfony\Component\HttpFoundation\Response
* An application/csv file download response object.
*/
public function template(string $migration_id) {
/** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
$migration = $this->pluginManagerMigration->getDefinition($migration_id);
if (empty($migration) || $migration['migration_group'] != 'farm_import_csv') {
throw new ResourceNotFoundException();
}
else {
$filename = str_replace(':', '--', $migration_id) . '.csv';
$response = new Response();
$response->headers->set('Content-Type', 'application/csv');
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"');
$column_names = [];
if (!empty($migration['third_party_settings']['farm_import_csv']['columns'])) {
foreach ($migration['third_party_settings']['farm_import_csv']['columns'] as $column) {
if (!empty($column['name'])) {
$column_names[] = $column['name'];
}
}
}
$response->setContent(implode(',', $column_names));
return $response;
}
}
}

View file

@ -82,9 +82,10 @@ class CsvImportTest extends FarmBrowserTestBase {
$this->assertSession()->pageTextNotContains('Migrate');
// Go to the asset, log, and term importers and confirm that column
// descriptions are included.
// descriptions are included, along with a link to download a template.
$this->drupalGet('import/csv/asset:equipment');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('Download template');
$log_columns = [
'name: Name of the asset. Required.',
'notes: Notes about the asset.',
@ -95,6 +96,7 @@ class CsvImportTest extends FarmBrowserTestBase {
}
$this->drupalGet('import/csv/log:harvest');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('Download template');
$log_columns = [
'name: Name of the log.',
'timestamp: Timestamp of the log. Accepts most date/time formats. Required.',
@ -110,6 +112,7 @@ class CsvImportTest extends FarmBrowserTestBase {
}
$this->drupalGet('import/csv/taxonomy_term:animal_type');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('Download template');
$log_columns = [
'name: Name of the term. Required.',
'description: Description of the term.',