Skip to content

Commit

Permalink
[BUGFIX] Ensure correct record type for new record in SuggestWizard
Browse files Browse the repository at this point in the history
FormEngine sends some information as payload with the suggest
wizard ajax request to the `SuggestWizardController` as context.
The controller loads the record based on the tableName and
the uid to determine the recordType to read the correct TCA field
configuration, respecting special overrides like `columnsOverrides`.

If a new record is created, there is no record uid available and
passed to the controller. Therefore, the recordType is not properly
determined. That leads to wrong record suggestions, if for example
the `allowed` record table is overriden for specific recordTypes.

This change adds the recordTypeValue as additional html data
attribute to the suggest search field, reads and sends it along
with the context payload in the ajax request to the suggest
wizard controller, which now uses the passed value while keeping
the record retrievement as fallback for now.

Suggest records are now directly searched correctly respecting
the full TCA configuration for the type, even for new records.

Additionally, uid is send as null instead of the string "NaN".

Used command(s):

> Build/Scripts/runTests.sh -s buildJavascript

Resolves: #101796
Releases: main, 12.4, 11.5
Change-Id: I3b814d37b7d4d3e9674ad6f2af882520c4f91413
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80773
Tested-by: Stefan B�rk <stefan@buerk.tech>
Reviewed-by: Stefan B�rk <stefan@buerk.tech>
Tested-by: core-ci <typo3@b13.com>
  • Loading branch information
sbuerk committed Aug 31, 2023
1 parent 5d193b1 commit 45e03be
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Build/Sources/TypeScript/backend/form-engine-suggest.ts
Expand Up @@ -81,18 +81,20 @@ class FormEngineSuggest {
return;
}

const uid = parseInt(target.dataset.uid, 10);
this.currentRequest = new AjaxRequest(TYPO3.settings.ajaxUrls.record_suggest);
this.currentRequest.post({
value: target.value,
tableName: target.dataset.tablename,
fieldName: target.dataset.fieldname,
uid: parseInt(target.dataset.uid, 10),
uid: (isNaN(uid) ? null : uid),
pid: parseInt(target.dataset.pid, 10),
dataStructureIdentifier: target.dataset.datastructureidentifier,
flexFormSheetName: target.dataset.flexformsheetname,
flexFormFieldName: target.dataset.flexformfieldname,
flexFormContainerName: target.dataset.flexformcontainername,
flexFormContainerFieldName: target.dataset.flexformcontainerfieldname,
recordTypeValue: target.dataset.recordtypevalue,
}).then(async (response: AjaxResponse): Promise<void> => {
const resultSet = await response.raw().text();
this.resultContainer.setAttribute('results', resultSet);
Expand Down
Expand Up @@ -54,6 +54,7 @@ public function searchAction(ServerRequestInterface $request): ResponseInterface
$flexFormFieldName = $parsedBody['flexFormFieldName'] ?? null;
$flexFormContainerName = $parsedBody['flexFormContainerName'] ?? null;
$flexFormContainerFieldName = $parsedBody['flexFormContainerFieldName'] ?? null;
$recordType = (string)($parsedBody['recordTypeValue'] ?? '');

// Determine TCA config of field
if (empty($dataStructureIdentifier)) {
Expand All @@ -62,8 +63,13 @@ public function searchAction(ServerRequestInterface $request): ResponseInterface
$fieldNameInPageTsConfig = $fieldName;

// With possible columnsOverrides
$row = BackendUtility::getRecord($tableName, $uid) ?? [];
$recordType = BackendUtility::getTCAtypeValue($tableName, $row);
// @todo Validate if we can move this fallback recordType determination, should be do-able in v13?!
if ($recordType === '') {
$recordType = BackendUtility::getTCAtypeValue(
$tableName,
BackendUtility::getRecord($tableName, $uid) ?? []
);
}
$columnsOverridesConfigOfField = $GLOBALS['TCA'][$tableName]['types'][$recordType]['columnsOverrides'][$fieldName]['config'] ?? null;
if ($columnsOverridesConfigOfField) {
ArrayUtility::mergeRecursiveWithOverrule($fieldConfig, $columnsOverridesConfigOfField);
Expand Down
4 changes: 4 additions & 0 deletions typo3/sysext/backend/Classes/Form/Element/GroupElement.php
Expand Up @@ -116,6 +116,7 @@ public function render()
$parameterArray = $this->data['parameterArray'];
$config = $parameterArray['fieldConf']['config'];
$elementName = $parameterArray['itemFormElName'];
$recordTypeValue = $this->data['recordTypeValue'] ?? null;

$selectedItems = $parameterArray['itemFormElValue'];
$maxItems = $config['maxitems'];
Expand Down Expand Up @@ -269,6 +270,9 @@ public function render()
$html[] = ' data-flexformfieldname="' . htmlspecialchars($flexFormFieldName) . '"';
$html[] = ' data-flexformcontainername="' . htmlspecialchars($flexFormContainerName) . '"';
$html[] = ' data-flexformcontainerfieldname="' . htmlspecialchars($flexFormContainerFieldName) . '"';
if ($recordTypeValue !== null && $recordTypeValue !== '') {
$html[] = ' data-recordtypevalue="' . htmlspecialchars($recordTypeValue) . '"';
}
$html[] = '/>';
$html[] = '</div>';
$html[] = '</div>';
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 45e03be

Please sign in to comment.