Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#5473 - Afform - ensure default values from DisplayOnly field… #31463

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,9 @@ public function preprocessSubmittedValues(array $submittedValues) {
$submittableFields = $this->getSubmittableFields($entity['fields']);
$fileFields = $this->getFileFields($entity['type'], $submittableFields);
foreach ($submittedValues[$entityName] ?? [] as $values) {
// Only accept values from fields on the form
$values['fields'] = array_intersect_key($values['fields'] ?? [], $submittableFields);
// Use default values from DisplayOnly fields + submittable fields on the form
$values['fields'] = $this->getForcedDefaultValues($entity['fields']) +
array_intersect_key($values['fields'] ?? [], $submittableFields);
// Unset prefilled file fields
foreach ($fileFields as $fileFieldName) {
if (isset($values['fields'][$fileFieldName]) && is_array($values['fields'][$fileFieldName])) {
Expand All @@ -623,7 +624,9 @@ public function preprocessSubmittedValues(array $submittedValues) {
// Enforce the limit set by join[max]
$joinValues = array_slice($joinValues, 0, $entity['joins'][$joinEntity]['max'] ?? NULL);
foreach ($joinValues as $index => $vals) {
$joinValues[$index] = array_intersect_key($vals, $allowedFields);
// As with the main entity, use default values from DisplayOnly fields + values from submittable fields
$joinValues[$index] = $this->getForcedDefaultValues($entity['joins'][$joinEntity]['fields'] ?? []);
$joinValues[$index] += array_intersect_key($vals, $allowedFields);
// Unset prefilled file fields
foreach ($fileFields as $fileFieldName) {
if (isset($joinValues[$index][$fileFieldName]) && is_array($joinValues[$index][$fileFieldName])) {
Expand Down Expand Up @@ -663,13 +666,38 @@ public function preprocessSubmittedValues(array $submittedValues) {
return $entityValues;
}

/**
* Return names of submittable fields (not DisplayOnly)
*
* TODO: Should filter out conditionally hidden fields too.
*
* @param array $fields
* @return array
*/
protected function getSubmittableFields(array $fields): array {
return array_filter($fields, function ($field) {
$inputType = $field['defn']['input_type'] ?? NULL;
return $inputType !== 'DisplayOnly';
});
}

/**
* Get default values from DisplayOnly fields
*
* @param array $fields
* @return array
*/
protected function getForcedDefaultValues(array $fields): array {
$values = [];
foreach ($fields as $field) {
$inputType = $field['defn']['input_type'] ?? NULL;
if ($inputType === 'DisplayOnly' && isset($field['defn']['afform_default'])) {
$values[$field['name']] = $field['defn']['afform_default'];
}
}
return $values;
}

/**
* Process form data
*/
Expand Down