Skip to content

Commit

Permalink
Allow columns to be selected for inclusion in CSV exports farmOS#842
Browse files Browse the repository at this point in the history
  • Loading branch information
mstenta committed May 9, 2024
1 parent 2399120 commit 43cc144
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- [Allow columns to be selected for inclusion in CSV exports #842](https://github.com/farmOS/farmOS/pull/842)

### Changed

- [Allow multiple locations to be referenced in the planting quick form #839](https://github.com/farmOS/farmOS/pull/839)
Expand Down
25 changes: 22 additions & 3 deletions modules/core/export/modules/csv/src/Form/EntityCsvActionForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,22 @@ public function buildForm(array $form, FormStateInterface $form_state, $entity_t
];
}

// Allow columns to be selected for inclusion.
$form['columns'] = [
'#type' => 'details',
'#title' => $this->t('Columns'),
'#tree' => TRUE,
];
$column_options = $this->getIncludeColumns($bundles);
$form['columns']['include'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Include columns'),
'#description' => $this->t('Which columns should be included in the CSV?'),
'#options' => array_combine($column_options, $column_options),
'#default_value' => $column_options,
'#required' => TRUE,
];

// Delegate to the parent method.
return parent::buildForm($form, $form_state);
}
Expand All @@ -273,7 +289,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$context = [

// Define the columns to include.
'include_columns' => $this->getIncludeColumns(),
'include_columns' => $form_state->getValue(['columns', 'include']),

// Return processed text from long text fields.
'processed_text' => TRUE,
Expand Down Expand Up @@ -339,10 +355,13 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
/**
* Get a list of columns to include in CSV exports.
*
* @param array|null $bundles
* A list of bundles to include. If omitted, all bundles will be used.
*
* @return string[]
* An array of column names.
*/
protected function getIncludeColumns() {
protected function getIncludeColumns($bundles = NULL) {

// Start with ID and UUID.
$columns = [
Expand Down Expand Up @@ -372,7 +391,7 @@ protected function getIncludeColumns() {
}

// Add bundle fields for supported field types.
$bundles = $this->entityTypeManager->getStorage($this->entityType->getBundleEntityType())->loadMultiple();
$bundles = $this->entityTypeManager->getStorage($this->entityType->getBundleEntityType())->loadMultiple($bundles);
foreach ($bundles as $bundle) {
if ($this->entityTypeManager->hasHandler($this->entityType->id(), 'bundle_plugin')) {
$bundle_fields = $this->entityTypeManager->getHandler($this->entityType->id(), 'bundle_plugin')->getFieldDefinitions($bundle->id());
Expand Down

0 comments on commit 43cc144

Please sign in to comment.