[
'renderType' => 'localizationStateSelector',
],
'otherLanguageContent' => [
'renderType' => 'otherLanguageContent',
'after' => [
'localizationStateSelector',
],
],
'defaultLanguageDifferences' => [
'renderType' => 'defaultLanguageDifferences',
'after' => [
'otherLanguageContent',
],
],
];
public function render(): array
{
$resultArray = $this->initializeResultArray();
$parameterArray = $this->data['parameterArray'];
$config = $parameterArray['fieldConf']['config'];
$readOnly = (bool)($config['readOnly'] ?? false);
$placeholder = trim((string)($config['placeholder'] ?? ''));
$enableCodeEditor = $config['enableCodeEditor'] ?? true;
$itemValue = '';
if (!empty($parameterArray['itemFormElValue'])) {
try {
$itemValue = (string)json_encode($parameterArray['itemFormElValue'], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR);
} catch (\JsonException) {
}
}
$width = null;
if ($config['cols'] ?? false) {
$width = $this->formMaxWidth(MathUtility::forceIntegerInRange($config['cols'], $this->minimumInputWidth, $this->maxInputWidth));
}
$rows = MathUtility::forceIntegerInRange(($config['rows'] ?? 5) ?: 5, 1, 20);
$originalRows = $rows;
if (($itemFormElementValueLength = strlen($itemValue)) > 80) {
$calculatedRows = MathUtility::forceIntegerInRange(
(int)round($itemFormElementValueLength / 40),
count(explode(LF, $itemValue)),
20
);
if ($originalRows < $calculatedRows) {
$rows = $calculatedRows;
}
}
$fieldId = StringUtility::getUniqueId('formengine-json-');
$fieldInformationResult = $this->renderFieldInformation();
$fieldInformationHtml = $fieldInformationResult['html'];
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
if ($readOnly && !$enableCodeEditor) {
$html = [];
$html[] = $this->renderLabel($fieldId);
$html[] = '
';
$resultArray['html'] = implode(LF, $html);
return $resultArray;
}
$itemName = (string)$parameterArray['itemFormElName'];
$attributes = [
'id' => $fieldId,
'name' => $itemName,
'wrap' => 'off',
'rows' => (string)$rows,
'class' => 'form-control font-monospace',
'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
];
if ($readOnly) {
$attributes['disabled'] = '';
}
if ($placeholder !== '') {
$attributes['placeholder'] = $placeholder;
}
// Use CodeMirror if available
if ($enableCodeEditor) {
// Compile and register code editor configuration
GeneralUtility::makeInstance(CodeEditor::class)->registerConfiguration();
$modeRegistry = GeneralUtility::makeInstance(ModeRegistry::class);
$mode = $modeRegistry->isRegistered('json')
? $modeRegistry->getByFormatCode('json')
: $modeRegistry->getDefaultMode();
$addons = $keymaps = [];
foreach (GeneralUtility::makeInstance(AddonRegistry::class)->getAddons() as $addon) {
foreach ($addon->getCssFiles() as $cssFile) {
$resultArray['stylesheetFiles'][] = $cssFile;
}
if (($module = $addon->getModule())) {
$addons[] = $module;
}
if (($keymap = $addon->getKeymap())) {
$keymaps[] = $keymap;
}
}
$codeMirrorConfig = [
'mode' => GeneralUtility::jsonEncodeForHtmlAttribute($mode->getModule(), false),
];
if ($readOnly) {
$codeMirrorConfig['readonly'] = '';
}
if ($placeholder !== '') {
$codeMirrorConfig['placeholder'] = $placeholder;
}
if ($addons !== []) {
$codeMirrorConfig['addons'] = GeneralUtility::jsonEncodeForHtmlAttribute($addons, false);
}
if ($keymaps !== []) {
$codeMirrorConfig['keymaps'] = GeneralUtility::jsonEncodeForHtmlAttribute($keymaps, false);
}
$resultArray['javaScriptModules'][] = JavaScriptModuleInstruction::create('@typo3/backend/code-editor/element/code-mirror-element.js');
$editorHtml = '
';
} else {
$attributes['class'] = implode(' ', array_merge(explode(' ', $attributes['class']), ['formengine-textarea', 't3js-enable-tab']));
$resultArray['javaScriptModules'][] = JavaScriptModuleInstruction::create('@typo3/backend/form-engine/element/json-element.js');
$editorHtml = '
';
}
$additionalHtml = [];
if (!$readOnly) {
$fieldControlResult = $this->renderFieldControl();
$fieldControlHtml = $fieldControlResult['html'];
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
if (!empty($fieldControlHtml)) {
$additionalHtml[] = '';
}
$fieldWizardResult = $this->renderFieldWizard();
$fieldWizardHtml = $fieldWizardResult['html'];
$resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
if (!empty($fieldWizardHtml)) {
$additionalHtml[] = '';
$additionalHtml[] = $fieldWizardHtml;
$additionalHtml[] = '
';
}
}
$html = [];
$html[] = '';
$resultArray['html'] = $this->wrapWithFieldsetAndLegend(implode(LF, $html));
return $resultArray;
}
}