From e74d28fa12ab6e2ed96159bb20f111804f34078b Mon Sep 17 00:00:00 2001 From: Boudewijn Schoon Date: Mon, 8 May 2023 16:27:13 +0200 Subject: [PATCH] Rename mapping from parent to parentId The attribute `parentId` is used in Craft to signify the value of the parent element, if any. Therefore, the mapping handle should also be `parentId`. This patch changes the mapping handle from `parent` into `parentId`. This allows the comparison code to properly verify that an existing parent already exists. Without this patch the current comparison will use the `parent` property that does not exist in a Craft entry. This should have been `parentId` which does exist. --- src/elements/Category.php | 2 +- src/elements/Entry.php | 2 +- ..._parent_to_parentid_for_feedme_imports.php | 70 +++++++++++++++++++ .../_includes/elements/categories/map.html | 2 +- .../_includes/elements/entries/map.html | 2 +- 5 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 src/migrations/m231025_081246_rename_parent_to_parentid_for_feedme_imports.php diff --git a/src/elements/Category.php b/src/elements/Category.php index c0631e5f..4e126be7 100644 --- a/src/elements/Category.php +++ b/src/elements/Category.php @@ -132,7 +132,7 @@ public function afterSave($data, $settings): void * @throws ElementNotFoundException * @throws Exception */ - protected function parseParent($feedData, $fieldInfo): ?int + protected function parseParentId($feedData, $fieldInfo): ?int { $value = $this->fetchSimpleValue($feedData, $fieldInfo); $default = DataHelper::fetchDefaultArrayValue($fieldInfo); diff --git a/src/elements/Entry.php b/src/elements/Entry.php index 4463fe58..1905d8d6 100644 --- a/src/elements/Entry.php +++ b/src/elements/Entry.php @@ -224,7 +224,7 @@ protected function parseExpiryDate($feedData, $fieldInfo): DateTime|bool|array|C * @throws ElementNotFoundException * @throws Exception */ - protected function parseParent($feedData, $fieldInfo): ?int + protected function parseParentId($feedData, $fieldInfo): ?int { $value = $this->fetchSimpleValue($feedData, $fieldInfo); $default = DataHelper::fetchDefaultArrayValue($fieldInfo); diff --git a/src/migrations/m231025_081246_rename_parent_to_parentid_for_feedme_imports.php b/src/migrations/m231025_081246_rename_parent_to_parentid_for_feedme_imports.php new file mode 100644 index 00000000..61d0e854 --- /dev/null +++ b/src/migrations/m231025_081246_rename_parent_to_parentid_for_feedme_imports.php @@ -0,0 +1,70 @@ +select(['id', 'fieldMapping']) + ->from('{{%feedme_feeds}}') + ->all(); + + foreach ($rows as $row) { + $fieldMapping = \json_decode($row['fieldMapping']); + + // Rename the `parent` argument into `parentId` if it exists + if (isset($fieldMapping->parent)) { + $fieldMapping->parentId = $fieldMapping->parent; + unset($fieldMapping->parent); + + $this->update( + '{{%feedme_feeds}}', + ['fieldMapping' => \json_encode($fieldMapping)], + ['id' => $row['id']] + ); + } + } + + return true; + } + + /** + * @inheritdoc + */ + public function safeDown() + { + $rows = (new Query()) + ->select(['id', 'fieldMapping']) + ->from('{{%feedme_feeds}}') + ->all(); + + foreach ($rows as $row) { + $fieldMapping = \json_decode($row['fieldMapping']); + + // Rename the `parentId` argument back into `parent` if it exists + if (isset($fieldMapping->parentId)) { + $fieldMapping->parent = $fieldMapping->parentId; + unset($fieldMapping->parentId); + + $this->update( + '{{%feedme_feeds}}', + ['fieldMapping' => \json_encode($fieldMapping)], + ['id' => $row['id']] + ); + } + } + + return true; + } +} diff --git a/src/templates/_includes/elements/categories/map.html b/src/templates/_includes/elements/categories/map.html index 60d40800..45479715 100644 --- a/src/templates/_includes/elements/categories/map.html +++ b/src/templates/_includes/elements/categories/map.html @@ -23,7 +23,7 @@ }, { type: 'categories', name: 'Parent', - handle: 'parent', + handle: 'parentId', instructions: 'Select a parent category to import these categories under.'|t('feed-me'), default: { type: 'elementselect', diff --git a/src/templates/_includes/elements/entries/map.html b/src/templates/_includes/elements/entries/map.html index cb7cd06a..1b77c59b 100644 --- a/src/templates/_includes/elements/entries/map.html +++ b/src/templates/_includes/elements/entries/map.html @@ -30,7 +30,7 @@ {% set fields = fields|push({ type: 'entries', name: 'Parent', - handle: 'parent', + handle: 'parentId', instructions: 'Select a parent entry to import these entries under.'|t('feed-me'), default: { type: 'elementselect',