Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Craft 5 processHeaderRow method breaks data tables for Twig Reports #336

Open
webrgp opened this issue Nov 4, 2024 · 0 comments
Open

Craft 5 processHeaderRow method breaks data tables for Twig Reports #336

webrgp opened this issue Nov 4, 2024 · 0 comments

Comments

@webrgp
Copy link

webrgp commented Nov 4, 2024

Description

In Craft 5, the processHeaderRow method (see below) breaks data tables for Twig Reports. I've tracked down the issue to this method:

public function processHeaderRow(&$rows): void
{
$labels = DataStudioModule::getInstance()->customTwigTemplates->labels;
// If we don't have default labels, we will use the first row as for our column headers
// We do so by making the first row the keys of the second row
if (empty($labels) && is_countable($rows) && count($rows) === 0) {
return;
}
$headerRow = [];
/** @var array $firstRowColumns */
$firstRowColumns = array_shift($rows);
if (is_countable($firstRowColumns) && count($firstRowColumns) > 0) {
$secondRow = array_shift($rows);
foreach ($firstRowColumns as $key => $column) {
$headerRow[$column] = $secondRow[$key];
}
}
array_unshift($rows, $headerRow);
}

Updating to the v4 version of the method seems to fixed for me, but it's probably worth double checking.

public function processHeaderRow(&$rows): void
{
$labels = DataStudioModule::getInstance()->customTwigTemplates->labels;
// If we don't have default labels, we will use the first row as for our column headers
// We do so by making the first row the keys of the second row
if (empty($labels) && (is_countable($rows) ? count($rows) : 0)) {
$headerRow = [];
/** @var array $firstRowColumns */
$firstRowColumns = array_shift($rows);
if (is_countable($firstRowColumns) ? count($firstRowColumns) : 0) {
$secondRow = array_shift($rows);
foreach ($firstRowColumns as $key => $column) {
$headerRow[$column] = $secondRow[$key];
}
}
array_unshift($rows, $headerRow);
}
}

How to reproduce

Just created a twig report using your example:

{# addHeaderRow accepts a single array with a comma-separated list of values #}
{% do sprout.twigDataSet.addHeaderRow(['Region', 'Trip Name']) %}

{# addRow, also accepts a single array with a comma-separated list of values. The array is defined by the opening and closing square brackets. #}
{% do sprout.twigDataSet.addRow([
  'Europe',
  'Camino de Santiago'
]) %}

{# addRows, accepts an array of arrays, where each array includes a comma-separated list of values. Note that there are two levels of opening and closing square brackets. #}
{% do sprout.twigDataSet.addRows([
    ['North America', 'John Muir Trail'],
    ['Asia', 'Langtang Trek']
]) %}

The data table will display the last 2 rows instead of 3.

Sprout Version

5.0.1

Craft Version

5.3.6

Database Type Version

MySQL 8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant