nodeFactory = $nodeFactory; } /** * Merge field information configuration with default and render them. * * @return array Result array */ protected function renderFieldInformation(): array { $options = $this->data; $fieldInformation = $this->defaultFieldInformation; $fieldInformationFromTca = $options['parameterArray']['fieldConf']['config']['fieldInformation'] ?? []; ArrayUtility::mergeRecursiveWithOverrule($fieldInformation, $fieldInformationFromTca); $options['renderType'] = 'fieldInformation'; $options['renderData']['fieldInformation'] = $fieldInformation; return $this->nodeFactory->create($options)->render(); } /** * Merge field control configuration with default controls and render them. * * @return array Result array */ protected function renderFieldControl(): array { $options = $this->data; $fieldControl = $this->defaultFieldControl; $fieldControlFromTca = $options['parameterArray']['fieldConf']['config']['fieldControl'] ?? []; ArrayUtility::mergeRecursiveWithOverrule($fieldControl, $fieldControlFromTca); $options['renderType'] = 'fieldControl'; $options['renderData']['fieldControl'] = $fieldControl; return $this->nodeFactory->create($options)->render(); } /** * Merge field wizard configuration with default wizards and render them. * * @return array Result array */ protected function renderFieldWizard(): array { $options = $this->data; $fieldWizard = $this->defaultFieldWizard; $fieldWizardFromTca = $options['parameterArray']['fieldConf']['config']['fieldWizard'] ?? []; ArrayUtility::mergeRecursiveWithOverrule($fieldWizard, $fieldWizardFromTca); $options['renderType'] = 'fieldWizard'; $options['renderData']['fieldWizard'] = $fieldWizard; return $this->nodeFactory->create($options)->render(); } /** * Render a label element for the current field by given id. */ protected function renderLabel(string $for): string { $label = htmlspecialchars($this->data['parameterArray']['fieldConf']['label'] ?? ''); if ($this->getBackendUser()->shallDisplayDebugInformation()) { $fieldName = $this->data['flexFormContainerFieldName'] ?? $this->data['flexFormFieldName'] ?? $this->data['containerFieldName'] ?? $this->data['fieldName']; $label .= ' [' . htmlspecialchars($fieldName) . ']'; } $html = '' . $label . ''; $html .= $this->renderDescription(); return $html; } /** * Elements that don't render a simple input field can't have a 'data['parameterArray']['fieldConf']['label'] ?? ''); if ($this->getBackendUser()->shallDisplayDebugInformation()) { $fieldName = $this->data['flexFormContainerFieldName'] ?? $this->data['flexFormFieldName'] ?? $this->data['containerFieldName'] ?? $this->data['fieldName']; $legend .= ' [' . htmlspecialchars($fieldName) . ']'; } $html = []; $html[] = ''; $html[] = '' . $legend . ''; $html[] = $this->renderDescription(); $html[] = $innerHTML; $html[] = ''; return implode(LF, $html); } protected function renderDescription(): string { $description = (string)($this->data['parameterArray']['fieldConf']['description'] ?? ''); if ($description === '') { return ''; } $description = $this->getLanguageService()->sL($description); if ($description === '') { return ''; } return '' . nl2br(htmlspecialchars($description)) . ''; } /** * Returns true if the "null value" checkbox should be rendered. This is used in some * "text" based types like "text" and "input" for some renderType's. * * A field has eval=null set, but has no useOverridePlaceholder defined. * Goal is to have a field that can distinct between NULL and empty string in the database. * A checkbox and an additional hidden field will be created, both with the same name * and prefixed with "control[active]". If the checkbox is set (value 1), the value from the casual * input field will be written to the database. If the checkbox is not set, the hidden field * transfers value=0 to DataHandler, the value of the input field will then be reset to NULL by the * DataHandler at an early point in processing, so NULL will be written to DB as field value. * * All that only works if the field is not within flex form scope since flex forms * can not store a "null" value or distinct it from "empty string". */ protected function hasNullCheckboxButNoPlaceholder(): bool { $hasNullCheckboxNoPlaceholder = false; $parameterArray = $this->data['parameterArray']; $mode = $parameterArray['fieldConf']['config']['mode'] ?? ''; if (empty($this->data['flexFormDataStructureIdentifier']) && ($parameterArray['fieldConf']['config']['nullable'] ?? false) && ($mode !== 'useOrOverridePlaceholder') ) { $hasNullCheckboxNoPlaceholder = true; } return $hasNullCheckboxNoPlaceholder; } /** * Returns true if the "null value" checkbox should be rendered and the placeholder * handling is enabled. This is used in some "text" based types like "text" and * "input" for some renderType's. * * A field has useOverridePlaceholder set and null in eval and is not within a flex form. * Here, a value from a deeper DB structure can be "fetched up" as value, and can also be overridden by a * local value. This is used in FAL, where eg. the "title" field can have the default value from sys_file_metadata, * the title field of sys_file_reference is then set to NULL. Or the "override" checkbox is set, and a string * or an empty string is then written to the field of sys_file_reference. * The situation is similar to hasNullCheckboxButNoPlaceholder(), but additionally a "default" value should be shown. * To achieve this, again a hidden control[hidden] field is added together with a checkbox with the same name * to transfer the information whether the default value should be used or not: Checkbox checked transfers 1 as * value in control[active], meaning the overridden value should be used. * Additionally to the casual input field, a second field is added containing the "placeholder" value. This * field has no name attribute and is not transferred at all. Those two are then hidden / shown depending * on the state of the above checkbox in via JS. */ protected function hasNullCheckboxWithPlaceholder(): bool { $hasNullCheckboxWithPlaceholder = false; $parameterArray = $this->data['parameterArray']; $mode = $parameterArray['fieldConf']['config']['mode'] ?? ''; if (empty($this->data['flexFormDataStructureIdentifier']) && ($parameterArray['fieldConf']['config']['nullable'] ?? false) && ($mode === 'useOrOverridePlaceholder') ) { $hasNullCheckboxWithPlaceholder = true; } return $hasNullCheckboxWithPlaceholder; } /** * Format field content if 'format' is set to date, filesize, ..., user * * @param string $format Configuration for the display. * @param ?string $itemValue The value to display * @param array $formatOptions Format options * @return string Formatted field value */ protected function formatValue($format, $itemValue, $formatOptions = []): string { switch ($format) { case 'date': if ($itemValue) { $option = isset($formatOptions['option']) ? trim($formatOptions['option']) : ''; if ($option) { if (isset($formatOptions['strftime']) && $formatOptions['strftime']) { $user = $this->getBackendUser(); if ($user->user['lang'] ?? false) { $locale = GeneralUtility::makeInstance(Locales::class)->createLocale($user->user['lang']); } else { $locale = new Locale(); } $value = (new DateFormatter())->strftime($option, (int)$itemValue, $locale); } else { $value = date($option, (int)$itemValue); } } else { $value = BackendUtility::date((int)$itemValue); } if (isset($formatOptions['appendAge']) && $formatOptions['appendAge']) { $now = DateTimeFactory::createFromTimestamp($GLOBALS['EXEC_TIME']); $then = DateTimeFactory::createFromTimestamp((int)$itemValue); $age = (new DateFormatter())->formatDateInterval( $now->diff($then), $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.minutesHoursDaysYears') ); $value .= ' (' . $age . ')'; } } else { $value = ''; } $itemValue = $value; break; case 'datetime': // compatibility with "eval" (type "input") if ($itemValue !== '' && $itemValue !== null) { $itemValue = BackendUtility::datetime((int)$itemValue); } break; case 'time': // compatibility with "eval" (type "input") if ($itemValue !== '' && $itemValue !== null) { $itemValue = BackendUtility::time((int)$itemValue, false); } break; case 'datetimesec': // compatibility with "eval" (type "input") if ($itemValue !== '' && $itemValue !== null) { $itemValue = BackendUtility::datetimesec((int)$itemValue); } break; case 'timesec': // compatibility with "eval" (type "input") if ($itemValue !== '' && $itemValue !== null) { $itemValue = BackendUtility::time((int)$itemValue); } break; case 'int': $baseArr = ['dec' => 'd', 'hex' => 'x', 'HEX' => 'X', 'oct' => 'o', 'bin' => 'b']; $base = isset($formatOptions['base']) ? trim($formatOptions['base']) : ''; $format = $baseArr[$base] ?? 'd'; $itemValue = sprintf('%' . $format, $itemValue); break; case 'float': // default precision $precision = 2; if (isset($formatOptions['precision'])) { $precision = MathUtility::forceIntegerInRange($formatOptions['precision'], 1, 10, $precision); } $itemValue = sprintf('%.' . $precision . 'f', $itemValue); break; case 'number': $format = isset($formatOptions['option']) ? '%' . trim($formatOptions['option']) : ''; $itemValue = sprintf($format, $itemValue); break; case 'md5': $itemValue = md5($itemValue); break; case 'filesize': // We need to cast to int here, otherwise empty values result in empty output, // but we expect zero. $value = GeneralUtility::formatSize((int)$itemValue); if (!empty($formatOptions['appendByteSize'])) { $value .= ' (' . $itemValue . ')'; } $itemValue = $value; break; case 'user': $func = trim($formatOptions['userFunc']); if ($func) { $params = [ 'value' => $itemValue, 'args' => $formatOptions['userFunc'], 'config' => [ 'type' => 'none', 'format' => $format, 'format.' => $formatOptions, ], ]; $itemValue = GeneralUtility::callUserFunction($func, $params, $this); } break; default: // Do nothing e.g. when $format === '' } // Make sure we have a string in the end. $itemValue could be null, for instance. return (string)$itemValue; } /** * Returns the max width in pixels for an elements like input and text * * @param int $size The abstract size value (1-48) * @return int Maximum width in pixels */ protected function formMaxWidth($size = 48) { $compensationForLargeDocuments = 1.33; $compensationForFormFields = 12; $compensatedSize = round($size * $compensationForLargeDocuments); return (int)ceil($compensatedSize * $compensationForFormFields); } /** * Handle custom javascript `eval` implementations. $evalObject is a hook object * for custom eval's. It is transferred to JS as a JavaScriptModuleInstruction if possible. * This is used by a couple of renderType's like various type="input", should * be used with care and is internal for now. * * @internal */ protected function resolveJavaScriptEvaluation(array $resultArray, string $name, ?object $evalObject): array { if (!is_object($evalObject) || !method_exists($evalObject, 'returnFieldJS')) { return $resultArray; } $javaScriptEvaluation = $evalObject->returnFieldJS(); if ($javaScriptEvaluation instanceof JavaScriptModuleInstruction) { // just use the module name and export-name $resultArray['javaScriptModules'][] = JavaScriptModuleInstruction::create( $javaScriptEvaluation->getName(), $javaScriptEvaluation->getExportName() )->invoke('registerCustomEvaluation', $name); } return $resultArray; } /*********************************************** * CheckboxElement related methods ***********************************************/ /** * Creates checkbox parameters * * @param string $itemName Form element name * @param int $formElementValue The value of the checkbox (representing checkboxes with the bits) * @param int $checkbox Checkbox # (0-9?) * @param int $checkboxesCount Total number of checkboxes in the array. * @param array $fieldChangeFuncs `fieldChangeFunc` items for client-side handling * @param bool $invert Inverts the state of the checkbox (but not of the bit value) * @return string either `onclick` attr or `data-formengine-field-change-*` attrs + possibly the checked-option set * @internal */ protected function checkBoxParams( string $itemName, int $formElementValue, int $checkbox, int $checkboxesCount, array $fieldChangeFuncs = [], bool $invert = false ): string { array_unshift($fieldChangeFuncs, new UpdateBitmaskOnFieldChange( $checkbox, $checkboxesCount, $invert, $itemName )); $checkboxPow = 2 ** $checkbox; $checked = $formElementValue & $checkboxPow; $attrs = $this->getOnFieldChangeAttrs('click', $fieldChangeFuncs); if ($checked xor $invert) { $attrs['checked'] = 'checked'; } $attrs['data-invert-state-display'] = $invert ? 'true' : 'false'; return GeneralUtility::implodeAttributes($attrs, true); } /** * Append the value of a form field to its label */ protected function appendValueToLabelInDebugMode(string|int $label, string|int $value): string { if ($value !== '' && $this->getBackendUser()->shallDisplayDebugInformation()) { return trim($label . ' [' . $value . ']'); } return trim((string)$label); } protected function getLanguageService(): LanguageService { return $GLOBALS['LANG']; } protected function getBackendUser(): BackendUserAuthentication { return $GLOBALS['BE_USER']; } }
[' . htmlspecialchars($fieldName) . ']