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 ddd4b1b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 37 deletions.
25 changes: 9 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);
static::removeNamespacesFromFields($form, $element->ID);

// Update and write the data object which will trigger model validation
$element->updateFromFormData($dataWithoutNamespaces);
$form->saveInto($element);
if ($element->isChanged()) {
try {
$element->write();
Expand All @@ -354,25 +353,19 @@ 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): void
{
$output = [];
$template = sprintf(EditFormFactory::FIELD_NAMESPACE_TEMPLATE, $elementID, '');
foreach ($data as $key => $value) {
$saveableFields = $form->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;
}

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 ddd4b1b

Please sign in to comment.