From 94cd28be79d0e7c3e7bfee23995f32e11a2fc6b3 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Wed, 6 Nov 2024 12:47:28 +1300 Subject: [PATCH] FIX Ensure files can be removed from elemental blocks --- src/Controllers/ElementalAreaController.php | 4 +++- src/Models/BaseElement.php | 12 +++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Controllers/ElementalAreaController.php b/src/Controllers/ElementalAreaController.php index 399a299e..3a208e5c 100644 --- a/src/Controllers/ElementalAreaController.php +++ b/src/Controllers/ElementalAreaController.php @@ -138,7 +138,9 @@ public function save(array $data, Form $form): HTTPResponse // Remove the namespace prefixes that were added by EditFormFactory $dataWithoutNamespaces = static::removeNamespacesFromFields($data, $element->ID); - // Update and write the data object which will trigger model validation + // Update and write the data object which will trigger model validation. + // Would usually be handled by $form->saveInto($element) but since the field names + // in the form have been namespaced, we need to handle it ourselves. $element->updateFromFormData($dataWithoutNamespaces); if ($element->isChanged()) { try { diff --git a/src/Models/BaseElement.php b/src/Models/BaseElement.php index 004d6d8a..7e35b3a6 100644 --- a/src/Models/BaseElement.php +++ b/src/Models/BaseElement.php @@ -656,15 +656,9 @@ public function getRenderTemplates($suffix = '') */ public function updateFromFormData($data) { - $cmsFields = $this->getCMSFields(); - - foreach ($data as $field => $datum) { - $field = $cmsFields->dataFieldByName($field); - - if (!$field) { - continue; - } - + $cmsFields = $this->getCMSFields()->saveableFields(); + foreach ($cmsFields as $fieldName => $field) { + $datum = $data[$fieldName] ?? null; $field->setSubmittedValue($datum); $field->saveInto($this); }