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

different approach to importing default vals for matrix #1282

Merged
merged 2 commits into from
Apr 17, 2023
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
58 changes: 42 additions & 16 deletions src/fields/Matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cake\Utility\Hash;
use craft\feedme\base\Field;
use craft\feedme\base\FieldInterface;
use craft\feedme\helpers\DataHelper;
use craft\feedme\Plugin;

/**
Expand Down Expand Up @@ -61,6 +62,7 @@ public function parseField()
foreach ($this->feedData as $nodePath => $value) {
// Get the field mapping info for this node in the feed
$fieldInfo = $this->_getFieldMappingInfoForNodePath($nodePath, $blocks);
$nodePathSegments = explode('/', $nodePath);

// If this is data concerning our Matrix field and blocks
if ($fieldInfo) {
Expand All @@ -69,20 +71,7 @@ public function parseField()
$subFieldInfo = $fieldInfo['subFieldInfo'];
$isComplexField = $fieldInfo['isComplexField'];

$nodePathSegments = explode('/', $nodePath);
$blockIndex = Hash::get($nodePathSegments, 1);

if (!is_numeric($blockIndex)) {
// Try to check if its only one-level deep (only importing one block type)
// which is particularly common for JSON.
$blockIndex = Hash::get($nodePathSegments, 2);

if (!is_numeric($blockIndex)) {
$blockIndex = 0;
}
}

$key = $blockIndex . '.' . $blockHandle . '.' . $subFieldHandle;
$key = $this->_getBlockKey($nodePathSegments, $blockHandle, $subFieldHandle);

// Check for complex fields (think Table, Super Table, etc), essentially anything that has
// sub-fields, and doesn't have data directly mapped to the field itself. It needs to be
Expand All @@ -108,6 +97,18 @@ public function parseField()
$fieldData[$key] = $parsedValue;
}
}

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);

$parsedValue = DataHelper::fetchSimpleValue($this->feedData, $fieldInfo);
$fieldData[$key] = $parsedValue;
}
}
}
}

// Handle some complex fields that don't directly have nodes, but instead have nested properties mapped.
Expand Down Expand Up @@ -184,10 +185,35 @@ public function parseField()
// Private Methods
// =========================================================================

/**
* Get block's key
*
* @param array $nodePathSegments
* @param string $blockHandle
* @param string $fieldHandle
* @return string
*/
private function _getBlockKey(array $nodePathSegments, string $blockHandle, string $fieldHandle): string
{
$blockIndex = Hash::get($nodePathSegments, 1);

if (!is_numeric($blockIndex)) {
// Try to check if its only one-level deep (only importing one block type)
// which is particularly common for JSON.
$blockIndex = Hash::get($nodePathSegments, 2);

if (!is_numeric($blockIndex)) {
$blockIndex = 0;
}
}

return $blockIndex . '.' . $blockHandle . '.' . $fieldHandle;
}

/**
* @param $nodePath
* @param $blocks
* @return array|null
* @return array|null|string
*/
private function _getFieldMappingInfoForNodePath($nodePath, $blocks)
{
Expand Down Expand Up @@ -216,7 +242,7 @@ private function _getFieldMappingInfoForNodePath($nodePath, $blocks)
}
}

if ($feedPath == $node || $node === 'usedefault') {
if ($feedPath == $node) {
return [
'blockHandle' => $blockHandle,
'subFieldHandle' => $subFieldHandle,
Expand Down