From 9ec3203e10bc21221de6195962a23529afc8f182 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Wed, 6 Nov 2024 15:02:58 +1300 Subject: [PATCH] API Remove custom logic in favour of Form::saveInto() --- src/Controllers/ElementalAreaController.php | 34 +++++++++++---------- src/Models/BaseElement.php | 21 ------------- 2 files changed, 18 insertions(+), 37 deletions(-) diff --git a/src/Controllers/ElementalAreaController.php b/src/Controllers/ElementalAreaController.php index db7a7886..562c20c8 100644 --- a/src/Controllers/ElementalAreaController.php +++ b/src/Controllers/ElementalAreaController.php @@ -298,8 +298,7 @@ public function getElementForm(): Form } /** - * Arrive here from FormRequestHandler::httpSubmission() during a POST request to - * /admin/linkfield/linkForm/ + * Arrive here from FormRequestHandler::httpSubmission() during a POST request. * The 'save' method is called because it is the FormAction set on the Form */ public function save(array $data, Form $form): HTTPResponse @@ -332,10 +331,10 @@ public function save(array $data, Form $form): HTTPResponse } // Remove the namespace prefixes that were added by EditFormFactory - $dataWithoutNamespaces = static::removeNamespacesFromFields($data, $element->ID); + $saveableForm = $this->removeNamespacesFromFields($form, $element->ID); // Update and write the data object which will trigger model validation - $element->updateFromFormData($dataWithoutNamespaces); + $saveableForm->saveInto($element); if ($element->isChanged()) { try { $element->write(); @@ -354,25 +353,28 @@ public function save(array $data, Form $form): HTTPResponse /** * Remove the pseudo namespaces that were added to form fields by the form factory - * - * @param array $data - * @param int $elementID - * @return array */ - public static function removeNamespacesFromFields(array $data, $elementID) + private function removeNamespacesFromFields(Form $form, int $elementID): Form { - $output = []; + // We need to clone the form and fields, or it will cause problems with the response + $clone = clone $form; + $fields = clone $clone->Fields(); + foreach ($fields as $field) { + $fields->replaceField($field->getName(), clone $field); + } + $clone->setFields($fields); + // Remove the namespace from the fields $template = sprintf(EditFormFactory::FIELD_NAMESPACE_TEMPLATE, $elementID, ''); - foreach ($data as $key => $value) { + $saveableFields = $fields->saveableFields(); + foreach ($saveableFields as $namespacedName => $field) { // Only look at fields that match the namespace template - if (substr($key ?? '', 0, strlen($template ?? '')) !== $template) { + if (substr($namespacedName, 0, strlen($template)) !== $template) { continue; } - - $fieldName = substr($key ?? '', strlen($template ?? '')); - $output[$fieldName] = $value; + $newName = substr($namespacedName, strlen($template)); + $field->setName($newName); } - return $output; + return $clone; } private function reorderElements(BaseElement $element, int $afterElementID): void diff --git a/src/Models/BaseElement.php b/src/Models/BaseElement.php index b2e59996..dc1adccf 100644 --- a/src/Models/BaseElement.php +++ b/src/Models/BaseElement.php @@ -646,27 +646,6 @@ public function getRenderTemplates($suffix = '') return $templateFlat; } - /** - * Given form data (wit - * - * @param $data - */ - public function updateFromFormData($data) - { - $cmsFields = $this->getCMSFields(); - - foreach ($data as $field => $datum) { - $field = $cmsFields->dataFieldByName($field); - - if (!$field) { - continue; - } - - $field->setSubmittedValue($datum); - $field->saveInto($this); - } - } - /** * Strip all namespaces from class namespace. *