Skip to content

Commit

Permalink
Merge branch '6.x' of https://github.com/craftcms/feed-me into 6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
angrybrad committed May 23, 2024
2 parents 5d5c73a + 35d8755 commit a88ee31
Showing 1 changed file with 35 additions and 14 deletions.
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

0 comments on commit a88ee31

Please sign in to comment.