fix(custom-fields) : propage le renommage d'un champ dans la formule de reference auto

This commit is contained in:
Matthieu
2026-05-11 16:22:28 +02:00
parent 5244698384
commit 9027917ea2
3 changed files with 120 additions and 0 deletions

View File

@@ -226,6 +226,13 @@ class SkeletonStructureService
}
if ($existingField) {
// Propagate rename to the parent ModelType's reference formula and required-fields list
// so existing `{oldName}` placeholders keep resolving after the field is renamed.
$oldName = $existingField->getName();
if ($oldName !== $normalized['name']) {
$this->propagateCustomFieldRename($modelType, $oldName, $normalized['name']);
}
// Update existing field
$existingField->setName($normalized['name']);
$existingField->setType($normalized['type']);
@@ -264,6 +271,38 @@ class SkeletonStructureService
}
}
private function propagateCustomFieldRename(ModelType $modelType, string $oldName, string $newName): void
{
$formula = $modelType->getReferenceFormula();
if (null !== $formula && '' !== $formula) {
$newFormula = preg_replace(
'/\{'.preg_quote($oldName, '/').'\}/',
'{'.$newName.'}',
$formula
);
if (null !== $newFormula && $newFormula !== $formula) {
$modelType->setReferenceFormula($newFormula);
}
}
$required = $modelType->getRequiredFieldsForReference();
if ($required) {
$changed = false;
$newRequired = [];
foreach ($required as $fieldName) {
if ($fieldName === $oldName) {
$newRequired[] = $newName;
$changed = true;
} else {
$newRequired[] = $fieldName;
}
}
if ($changed) {
$modelType->setRequiredFieldsForReference($newRequired);
}
}
}
/**
* Normalize frontend custom field data to a common shape.
*