[FR]: Abstract custom field processing in EntriesController::setFieldValuesFromRequest into it's own method. #13252
-
When building custom forms using a form builder like Symfony forms component it isn't always possible to use a namespace like Can we split the fetching of data from the request and the processing into two separate methods like so: /**
* @inheritdoc
*/
public function setFieldValuesFromRequest(string $paramNamespace = ''): void
{
$this->setFieldParamNamespace($paramNamespace);
$data = Craft::$app->getRequest()->getBodyParam($paramNamespace, []);
$this->setFieldValuesFromData($data);
}
public function setFieldValuesFromData(mixed $data): void
{
// Run through this multiple times, in case any fields become visible as a result of other field value changes
$processedFields = [];
do {
$processedAnyFields = false;
foreach ($this->fieldLayoutFields(true) as $field) {
// Have we already processed this field?
if (isset($processedFields[$field->id])) {
continue;
}
$processedFields[$field->id] = true;
$processedAnyFields = true;
// Do we have any post data for this field?
if (isset($values[$field->handle])) {
$value = $values[$field->handle];
} elseif (
isset($this->_fieldParamNamePrefix) &&
$this->_fieldParamNamePrefix !== '' &&
UploadedFile::getInstancesByName("$this->_fieldParamNamePrefix.$field->handle")
) {
// A file was uploaded for this field
$value = null;
} else {
continue;
}
// Normalize it now in case the system language changes later
// (we'll do this with the value directly rather than using setFieldValue() + normalizeFieldValue(),
// because it's slightly more efficient and to workaround an infinite loop bug caused by Matrix
// needing to render an object template on the owner element during normalization, which would in turn
// cause the Matrix field value to be (re-)normalized based on the POST data, and on and on...)
$value = $field->normalizeValue($value, $this);
$this->setFieldValue($field->handle, $value);
$this->_normalizedFieldValues[$field->handle] = true;
}
} while ($processedAnyFields);
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Craft 4.4.14 is out with improved handling for empty namespaces passed to |
Beta Was this translation helpful? Give feedback.
Craft 4.4.14 is out with improved handling for empty namespaces passed to
Element::setValuesFromRequest()
.setFieldValues()
can generally be used for everything else.