Skip to content

Maintain field settings when switching field types #16783

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

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Administration
- Added the `--batch-size` option for `resave/*` commands. ([#16586](https://github.com/craftcms/cms/issues/16586))
- Dragging headings within the Customize Sources modal now also drags any subsequent sources. ([#16737](https://github.com/craftcms/cms/issues/16737))
- When switching field types, any field settings which are defined by the same base class are now preserved. ([#16783](https://github.com/craftcms/cms/pull/16783))

### Extensibility
- Global nav items and breadcrumbs can now have `aria-label` attributes via an `ariaLabel` property.
Expand Down
26 changes: 26 additions & 0 deletions src/controllers/FieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
use craft\fields\MissingField;
use craft\fields\PlainText;
use craft\helpers\ArrayHelper;
use craft\helpers\Component;
use craft\helpers\UrlHelper;
use craft\models\FieldGroup;
use craft\models\FieldLayoutTab;
use craft\web\assets\fieldsettings\FieldSettingsAsset;
use craft\web\Controller;
use ReflectionException;
use ReflectionProperty;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;
Expand Down Expand Up @@ -292,9 +295,32 @@ public function actionRenderSettings(): Response
$this->requirePostRequest();
$this->requireAcceptsJson();

/** @var class-string<FieldInterface> $type */
$type = $this->request->getRequiredBodyParam('type');
$field = Craft::$app->getFields()->createField($type);

/** @var class-string<FieldInterface>|null $oldType */
$oldType = $this->request->getBodyParam('oldType');
if ($oldType && Component::validateComponentClass($oldType, FieldInterface::class)) {
$settingsStr = $this->request->getBodyParam('settings');
parse_str($settingsStr, $postedOldSettings);
$oldNamespace = $this->request->getBodyParam('oldNamespace');
$settings = ArrayHelper::getValue($postedOldSettings, $oldNamespace, []);

// Remove any settings that aren't defined by the same class between both types
$settings = array_filter($settings, function($attribute) use ($type, $oldType) {
try {
$r1 = new ReflectionProperty($type, $attribute);
$r2 = new ReflectionProperty($oldType, $attribute);
return $r1->getDeclaringClass()->name === $r2->getDeclaringClass()->name;
} catch (ReflectionException) {
return false;
}
}, ARRAY_FILTER_USE_KEY);

Craft::configure($field, $settings);
}

$view = Craft::$app->getView();
$html = $view->renderTemplate('settings/fields/_type-settings.twig', [
'field' => $field,
Expand Down
2 changes: 1 addition & 1 deletion src/web/assets/fieldsettings/dist/fieldsettings.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/web/assets/fieldsettings/dist/fieldsettings.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions src/web/assets/fieldsettings/src/fieldsettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@
});
}

const oldType = this.currentType;
const settings = $('<form/>')
.append(this.$container.clone())
.serialize();

// Save & detach the current settings
this.typeSettings[this.currentType] = this.$container
.children()
.detach();
this.typeSettings[oldType] = this.$container.children().detach();

this.currentType = this.$toggle.val();

Expand All @@ -54,12 +57,15 @@

let data = {
type: this.currentType,
oldType,
settings,
};
if (this.namespace) {
data.namespace = this.namespace.replace(
/__TYPE__/g,
this.currentType
);
data.oldNamespace = this.namespace.replace(/__TYPE__/g, oldType);
}

Craft.sendActionRequest('POST', 'fields/render-settings', {
Expand Down