forked from farmOS/farmOS
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix incorrect CSV headers when exporting multiple record types farmOS…
- Loading branch information
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Drupal\farm_csv\Encoder; | ||
|
||
use Drupal\csv_serialization\Encoder\CsvEncoder as ContribCsvEncoder; | ||
|
||
/** | ||
* Extend the contrib CsvEncoder class with custom header extraction logic. | ||
*/ | ||
class CsvEncoder extends ContribCsvEncoder { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function extractHeaders(array $data, array $context = []) { | ||
$headers = []; | ||
|
||
// Iterate over each row and merge its headers. | ||
// This differs from the parent method in that it combines headers from all | ||
// rows instead of only using headers from the first row. | ||
foreach ($data as $row) { | ||
$headers = array_merge($headers, array_diff(array_keys($row), $headers)); | ||
} | ||
|
||
// Use labels provided by the Views style plugin, if available. | ||
// This mimics the logic of the parent method for compatibility in Views | ||
// contexts. | ||
if (!empty($context['views_style_plugin'])) { | ||
$fields = $context['views_style_plugin']->view->field; | ||
$headers = array_map(function ($header) use ($fields) { | ||
return !empty($fields[$header]->options['label']) ? $fields[$header]->options['label'] : $header; | ||
}, $headers); | ||
} | ||
|
||
return $headers; | ||
} | ||
|
||
} |