Skip to content

Commit

Permalink
Merge pull request #1385 from craftcms/bugfix/1375-empty-headings-fal…
Browse files Browse the repository at this point in the history
…lback

Bugfix/1375 empty headings fallback
  • Loading branch information
angrybrad authored Nov 25, 2023
2 parents 56da484 + dd84989 commit 9db43f8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/datatypes/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,14 @@ public function getFeed($url, $settings, $usePrimaryElement = true)
$filteredRow = [];

// Additional work here to handle line-breaks in keys (CSV header) - they're not allowed
$col = 1;
foreach ($row as $key => $value) {
$newKey = preg_replace('#\r\n?#', " ", $key);
if (trim($newKey) == '') {
$newKey = 'blank_heading_' . $col;
}
$filteredRow[$newKey] = $value;
$col++;
}

// Check for empty rows - ditch them
Expand Down
4 changes: 4 additions & 0 deletions src/datatypes/GoogleSheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public function getFeed($url, $settings, $usePrimaryElement = true)
foreach ($row as $j => $column) {
$key = $headers[$j];

if (trim($key) == '') {
$key = 'blank_heading_' . ($j + 1);
}

$array[$i][$key] = $column;
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/datatypes/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ public function getFeed($url, $settings, $usePrimaryElement = true)
return ['success' => false, 'error' => $error];
}

// if we have empty keys in the array - throw an error, that's not allowed
$containsEmptyKeys = false;
array_walk_recursive($array, function($value, $key) use (&$containsEmptyKeys) {
if (trim($key) === '') {
$containsEmptyKeys = true;
}
});
if ($containsEmptyKeys) {
$error = 'Invalid Data: data contains empty headings (keys)';
Plugin::error($error);
return ['success' => false, 'error' => $error];
}

// If using pagination, set it up here - we need to do this before messing around with the primary element
$this->setupPaginationUrl($array, $settings);

Expand Down
6 changes: 6 additions & 0 deletions src/templates/feeds/_map.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
<h2>{{ 'Unable to proceed to field mapping'|t('feed-me') }}</h2>
<p>{{ 'Feed Me is unable to find, or parse your provided data. This usually means your URL cannot be reached from your Craft site, or your {feedType} is invalid. Check the logs, and double-check your settings.'|t('feed-me', { feedType: feed.feedType|upper }) }}</p>

{% if feedMappingData.error is not empty %}
<div class="fullpage-error-message">
<code>{{ feedMappingData.error }}</code>
</div>
{% endif %}

<div class="buttons">
<a href="{{ url('feed-me/feeds/' ~ feed.id) }}" class="btn submit">&larr; {{ 'Back to feed'|t('feed-me') }}</a>
<a href="{{ url('feed-me/logs') }}" class="btn submit">{{ 'Go to logs'|t('feed-me') }}</a>
Expand Down

0 comments on commit 9db43f8

Please sign in to comment.