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

amends for various bare bone matrix scenarios #1452

Merged
merged 1 commit into from
May 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions src/fields/Matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,18 @@ public function parseField(): mixed
}

foreach ($blocks as $blockHandle => $fields) {
foreach ($fields['fields'] as $fieldHandle => $fieldInfo) {
$node = Hash::get($fieldInfo, 'node');
if ($node === 'usedefault') {
$key = $this->_getBlockKey($nodePathSegments, $blockHandle, $fieldHandle);
if (isset($fields['fields'])) {
foreach ($fields['fields'] as $fieldHandle => $fieldInfo) {
$node = Hash::get($fieldInfo, 'node');
if ($node === 'usedefault') {
$key = $this->_getBlockKey($nodePathSegments, $blockHandle, $fieldHandle);

$parsedValue = DataHelper::fetchSimpleValue($this->feedData, $fieldInfo);
$fieldData[$key] = $parsedValue;
$parsedValue = DataHelper::fetchSimpleValue($this->feedData, $fieldInfo);
$fieldData[$key] = $parsedValue;
}
}
}
if ($attributeInfo) {
if (isset($fields['attributes'])) {
foreach ($fields['attributes'] as $fieldHandle => $fieldInfo) {
$node = Hash::get($fieldInfo, 'node');
if ($node === 'usedefault') {
Expand Down Expand Up @@ -190,11 +192,25 @@ public function parseField(): mixed

foreach ($attributeData as $blockSubFieldHandle => $value) {
$handles = explode('.', $blockSubFieldHandle);
$blockHandle = $handles[1];
// Inclusion of block handle here prevents blocks of different types from being merged together
$blockIndex = 'new' . $handles[1] . ((int)$handles[0] + 1);
$blockIndex = 'new' . $blockHandle . ((int)$handles[0] + 1);
$subFieldHandle = $handles[2];

$preppedData[$blockIndex . '.' . $subFieldHandle] = $value;

// if type, enabled and collapsed are not set, set them now;
// this can happen if we have a matrix entry with just the title and no custom fields
if (!isset($preppedData[$blockIndex . '.type'])) {
$preppedData[$blockIndex . '.type'] = $blockHandle;
}
if (!isset($preppedData[$blockIndex . '.enabled'])) {
$disabled = Hash::get($this->fieldInfo, 'blocks.' . $blockHandle . '.disabled', false);
$preppedData[$blockIndex . '.enabled'] = !$disabled;
}
if (!isset($preppedData[$blockIndex . '.collapsed'])) {
$preppedData[$blockIndex . '.collapsed'] = false;
}
}

// if there's nothing in the prepped data, return null, as if mapping doesn't exist
Expand All @@ -209,12 +225,17 @@ public function parseField(): mixed
$resultBlocks = [];
foreach ($expanded as $blockData) {
// all the fields are empty and setEmptyValues is off, ignore the block
if (
!empty(array_filter(
$blockData['fields'],
fn($value) => (is_string($value) && !empty($value)) || (is_array($value) && !empty(array_filter($value)))
))
) {
if (isset($blockData['fields'])) {
if (
!empty(array_filter(
$blockData['fields'],
fn($value) => (is_string($value) && !empty($value)) || (is_array($value) && !empty(array_filter($value)))
))
) {
$resultBlocks['new' . $index++] = $blockData;
}
} else {
// if there are no fields in the block data, we can still have just the attributes, e.g. just the title
$resultBlocks['new' . $index++] = $blockData;
}
}
Expand Down
Loading