Skip to content

Commit

Permalink
FIX Two CMSField fixes:
Browse files Browse the repository at this point in the history
- Fields that are also request handlers are now supported
- Fields that have submitted values different from saved values are now supported
  • Loading branch information
ScopeyNZ committed Oct 28, 2018
1 parent 5e7f165 commit 4b00e59
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
15 changes: 15 additions & 0 deletions src/Controllers/ElementalAreaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ class ElementalAreaController extends CMSMain
private static $url_handlers = [
// API access points with structured data
'POST api/saveForm/$ID' => 'apiSaveForm',
'POST $FormName/field/$FieldName' => 'formAction',
];

private static $allowed_actions = [
'elementForm',
'schema',
'apiSaveForm',
'formAction',
];

public function getClientConfig()
Expand Down Expand Up @@ -163,6 +165,19 @@ public function apiSaveForm(HTTPRequest $request)
return HTTPResponse::create($body)->addHeader('Content-Type', 'application/json');
}

public function formAction(HTTPRequest $request)
{
$formName = $request->param('FormName');

// Get the element ID from the form name
$id = substr($formName, strlen(sprintf(self::FORM_NAME_TEMPLATE, '')));
$form = $this->getElementForm($id);

$field = $form->getRequestHandler()->handleField($request);

return $field->handleRequest($request);
}

/**
* Remove the pseudo namespaces that were added to form fields by the form factory
*
Expand Down
14 changes: 10 additions & 4 deletions src/Forms/ElementalAreaField.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,21 +241,27 @@ public function saveInto(DataObjectInterface $dataObject)
continue;
}

$fields = [];

$fieldNamePrefix = sprintf(EditFormFactory::FIELD_NAMESPACE_TEMPLATE, $elementId, '');
$prefixLength = strlen($fieldNamePrefix);

$cmsFields = $element->getCMSFields();

foreach ($data as $field => $datum) {
// Check that the field starts with a valid name
if (strpos($field, $fieldNamePrefix) !== 0) {
continue;
}

$fields[substr($field, $prefixLength)] = $datum;
$field = $cmsFields->dataFieldByName(substr($field, $prefixLength));

if (!$field) {
continue;
}

$field->setSubmittedValue($datum);
$field->saveInto($element);
}

$element->update($fields);
$element->write();
}
}
Expand Down

0 comments on commit 4b00e59

Please sign in to comment.