Skip to content

Commit

Permalink
API Remove custom logic in favour of Form::saveInto()
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Nov 6, 2024
1 parent eadd5ad commit 9ec3203
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
34 changes: 18 additions & 16 deletions src/Controllers/ElementalAreaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,7 @@ public function getElementForm(): Form
}

/**
* Arrive here from FormRequestHandler::httpSubmission() during a POST request to
* /admin/linkfield/linkForm/<LinkID>
* 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
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down
21 changes: 0 additions & 21 deletions src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 9ec3203

Please sign in to comment.