diff --git a/src/fields/Matrix.php b/src/fields/Matrix.php index 7b44224e..9354e48d 100644 --- a/src/fields/Matrix.php +++ b/src/fields/Matrix.php @@ -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; /** @@ -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) { @@ -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 @@ -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. @@ -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) { @@ -216,7 +242,7 @@ private function _getFieldMappingInfoForNodePath($nodePath, $blocks) } } - if ($feedPath == $node || $node === 'usedefault') { + if ($feedPath == $node) { return [ 'blockHandle' => $blockHandle, 'subFieldHandle' => $subFieldHandle,