Skip to content

Commit

Permalink
Rename mapping from parent to parentId
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
boudewijn-zicht committed Oct 25, 2023
1 parent eaac8e3 commit e74d28f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/elements/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/elements/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace craft\feedme\migrations;

use craft\db\Migration;
use craft\db\Query;

/**
* m231025_081246_rename_parent_to_parentid_for_feedme_imports migration.
*/
class m231025_081246_rename_parent_to_parentid_for_feedme_imports extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{
$rows = (new Query())
->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;
}
}
2 changes: 1 addition & 1 deletion src/templates/_includes/elements/categories/map.html
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/templates/_includes/elements/entries/map.html
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit e74d28f

Please sign in to comment.