[!!!][FEATURE] FormEngine element level refactoring
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / InputColorPickerElement.php
index 2bb6895..f7c83a8 100644 (file)
@@ -14,152 +14,228 @@ namespace TYPO3\CMS\Backend\Form\Element;
  * The TYPO3 project - inspiring people to share!
  */
 
-use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\MathUtility;
 use TYPO3\CMS\Core\Utility\StringUtility;
+use TYPO3\CMS\Lang\LanguageService;
 
 /**
- * Class InputColorPickerElement
+ * Render an input field with a color picker
  */
 class InputColorPickerElement extends AbstractFormElement
 {
     /**
+     * Default field wizards enabled for this element.
+     *
+     * @var array
+     */
+    protected $defaultFieldWizard = [
+        'otherLanguageContent' => [
+            'renderType' => 'otherLanguageContent',
+        ],
+        'defaultLanguageDifferences' => [
+            'renderType' => 'defaultLanguageDifferences',
+            'after' => [
+                'otherLanguageContent',
+            ],
+        ],
+    ];
+
+    /**
      * This will render a single-line input form field, possibly with various control/validation features
      *
      * @return array As defined in initializeResultArray() of AbstractNode
      */
     public function render()
     {
+        $languageService = $this->getLanguageService();
+
         $table = $this->data['tableName'];
         $fieldName = $this->data['fieldName'];
         $row = $this->data['databaseRow'];
         $parameterArray = $this->data['parameterArray'];
         $resultArray = $this->initializeResultArray();
 
+        $itemValue = $parameterArray['itemFormElValue'];
         $config = $parameterArray['fieldConf']['config'];
-        $specConf = BackendUtility::getSpecConfParts($parameterArray['fieldConf']['defaultExtras']);
         $size = MathUtility::forceIntegerInRange($config['size'] ?: $this->defaultInputWidth, $this->minimumInputWidth, $this->maxInputWidth);
         $evalList = GeneralUtility::trimExplode(',', $config['eval'], true);
-        $classes = [];
-        $attributes = [];
+        $width = (int)$this->formMaxWidth($size);
+        $nullControlNameAttribute = ' name="' . htmlspecialchars('control[active][' . $table . '][' . $row['uid'] . '][' . $fieldName . ']') . '"';
 
-        // readonly
         if ($config['readOnly']) {
-            $itemFormElValue = $parameterArray['itemFormElValue'];
-            $options = $this->data;
-            $options['parameterArray'] = [
-                'fieldConf' => [
-                    'config' => $config,
-                ],
-                'itemFormElValue' => $itemFormElValue,
-            ];
-            $options['renderType'] = 'none';
-            return $this->nodeFactory->create($options)->render();
+            $html = [];
+            $html[] = '<div class="t3js-formengine-field-item">';
+            $html[] =   '<div class="form-wizards-wrap">';
+            $html[] =       '<div class="form-wizards-element">';
+            $html[] =           '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
+            $html[] =               '<input class="form-control" value="' . htmlspecialchars($itemValue) . '" type="text" disabled>';
+            $html[] =           '</div>';
+            $html[] =       '</div>';
+            $html[] =   '</div>';
+            $html[] = '</div>';
+            $resultArray['html'] = implode(LF, $html);
+            return $resultArray;
         }
 
         // @todo: The whole eval handling is a mess and needs refactoring
         foreach ($evalList as $func) {
-            switch ($func) {
-                case 'required':
-                    $attributes['data-formengine-validation-rules'] = $this->getValidationDataAsJsonString(['required' => true]);
-                    break;
-                default:
-                    // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
-                    // @todo: keyword like "date", or a class reference. The global registration could be dropped then
-                    // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
-                    if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
-                        if (class_exists($func)) {
-                            $evalObj = GeneralUtility::makeInstance($func);
-                            if (method_exists($evalObj, 'deevaluateFieldValue')) {
-                                $_params = [
-                                    'value' => $parameterArray['itemFormElValue']
-                                ];
-                                $parameterArray['itemFormElValue'] = $evalObj->deevaluateFieldValue($_params);
-                            }
-                        }
+            // @todo: This is ugly: The code should find out on it's own whether a eval definition is a
+            // @todo: keyword like "date", or a class reference. The global registration could be dropped then
+            // Pair hook to the one in \TYPO3\CMS\Core\DataHandling\DataHandler::checkValue_input_Eval()
+            if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func])) {
+                if (class_exists($func)) {
+                    $evalObj = GeneralUtility::makeInstance($func);
+                    if (method_exists($evalObj, 'deevaluateFieldValue')) {
+                        $_params = [
+                            'value' => $itemValue
+                        ];
+                        $itemValue = $evalObj->deevaluateFieldValue($_params);
                     }
+                    if (method_exists($evalObj, 'returnFieldJS')) {
+                        $resultArray['additionalJavaScriptPost'][] = 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($evalData) . '] = function(value) {' . $evalObj->returnFieldJS() . '};';
+                    }
+                }
             }
         }
 
-        $paramsList = [
-            'field' => $parameterArray['itemFormElName'],
-            'evalList' => implode(',', $evalList),
-            'is_in' => trim($config['is_in']),
-        ];
-
-        // set classes
-        $classes[] = 'form-control';
-        $classes[] = 'hasDefaultValue';
-        $classes[] = 't3js-clearable';
-        $classes[] = 't3js-color-picker';
-        $classes[] = 'formengine-colorpickerelement';
-        $attributes['class'] = implode(' ', $classes);
-
         // Load needed js library
         $resultArray['requireJsModules'][] = [
             'TYPO3/CMS/Backend/ColorPicker' => 'function(ColorPicker){ColorPicker.initialize()}'
         ];
 
-        // calculate attributes
-        $attributes['data-formengine-validation-rules'] = $this->getValidationDataAsJsonString($config);
-        $attributes['data-formengine-input-params'] = json_encode($paramsList);
-        $attributes['data-formengine-input-name'] = $parameterArray['itemFormElName'];
-        $attributes['id'] = StringUtility::getUniqueId('formengine-input-');
-        $attributes['value'] = '';
+        $attributes = [
+            'value' => $itemValue,
+            'id' => StringUtility::getUniqueId('formengine-input-'),
+            'class' => implode(' ', [
+                'form-control',
+                'hasDefaultValue',
+                't3js-clearable',
+                't3js-color-picker',
+                'formengine-colorpickerelement',
+            ]),
+            'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
+            'data-formengine-input-params' => json_encode([
+                'field' => $parameterArray['itemFormElName'],
+                'evalList' => implode(',', $evalList),
+                'is_in' => trim($config['is_in']),
+            ]),
+            'data-formengine-input-name' => $parameterArray['itemFormElName'],
+        ];
+
         if (isset($config['max']) && (int)$config['max'] > 0) {
             $attributes['maxlength'] = (int)$config['max'];
         }
-
-        // This is the EDITABLE form field.
         if (!empty($config['placeholder'])) {
             $attributes['placeholder'] = trim($config['placeholder']);
         }
-
         if (isset($config['autocomplete'])) {
             $attributes['autocomplete'] = empty($config['autocomplete']) ? 'new-' . $fieldName : 'on';
         }
 
-        // Build the attribute string
-        $attributeString = '';
-        foreach ($attributes as $attributeName => $attributeValue) {
-            $attributeString .= ' ' . $attributeName . '="' . htmlspecialchars($attributeValue) . '"';
-        }
-
-        $html = '<input type="text" value="' . htmlspecialchars($parameterArray['itemFormElValue']) . '" ' . $attributeString . ' />';
-
-        // This is the ACTUAL form field - values from the EDITABLE field must be transferred to this field which is the one that is written to the database.
-        $html .= '<input type="hidden" name="' . htmlspecialchars($parameterArray['itemFormElName']) . '" value="' . htmlspecialchars($parameterArray['itemFormElValue']) . '" />';
-
-        // Going through all custom evaluations configured for this field
-        // @todo: Similar to above code!
-        foreach ($evalList as $evalData) {
-            if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$evalData])) {
-                if (class_exists($evalData)) {
-                    $evalObj = GeneralUtility::makeInstance($evalData);
-                    if (method_exists($evalObj, 'returnFieldJS')) {
-                        $resultArray['additionalJavaScriptPost'][] = 'TBE_EDITOR.customEvalFunctions[' . GeneralUtility::quoteJSvalue($evalData) . '] = function(value) {' . $evalObj->returnFieldJS() . '};';
-                    }
+        $legacyWizards = $this->renderWizards();
+        $legacyFieldControlHtml = implode(LF, $legacyWizards['fieldControl']);
+        $legacyFieldWizardHtml = implode(LF, $legacyWizards['fieldWizard']);
+
+        $fieldInformationResult = $this->renderFieldInformation();
+        $fieldInformationHtml = $fieldInformationResult['html'];
+        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
+
+        $fieldWizardResult = $this->renderFieldWizard();
+        $fieldWizardHtml = $legacyFieldWizardHtml . $fieldWizardResult['html'];
+        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
+
+        $fieldControlResult = $this->renderFieldControl();
+        $fieldControlHtml = $legacyFieldControlHtml . $fieldControlResult['html'];
+        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
+
+        $mainFieldHtml = [];
+        $mainFieldHtml[] = '<div class="form-control-wrap" style="max-width: ' . $width . 'px">';
+        $mainFieldHtml[] =  '<div class="form-wizards-wrap">';
+        $mainFieldHtml[] =      '<div class="form-wizards-element">';
+        $mainFieldHtml[] =          '<input type="text"' . GeneralUtility::implodeAttributes($attributes, true) . ' />';
+        $mainFieldHtml[] =          '<input type="hidden" name="' . $parameterArray['itemFormElName'] . '" value="' . htmlspecialchars($itemValue) . '" />';
+        $mainFieldHtml[] =      '</div>';
+        $mainFieldHtml[] =      '<div class="form-wizards-items-aside">';
+        $mainFieldHtml[] =          '<div class="btn-group">';
+        $mainFieldHtml[] =              $fieldControlHtml;
+        $mainFieldHtml[] =          '</div>';
+        $mainFieldHtml[] =      '</div>';
+        $mainFieldHtml[] =      '<div class="form-wizards-items-bottom">';
+        $mainFieldHtml[] =          $fieldWizardHtml;
+        $mainFieldHtml[] =      '</div>';
+        $mainFieldHtml[] =  '</div>';
+        $mainFieldHtml[] = '</div>';
+        $mainFieldHtml = implode(LF, $mainFieldHtml);
+
+        $fullElement = $mainFieldHtml;
+        if ($this->hasNullCheckboxButNoPlaceholder()) {
+            $checked = $itemValue !== null ? ' checked="checked"' : '';
+            $fullElement = [];
+            $fullElement[] = '<div class="t3-form-field-disable"></div>';
+            $fullElement[] = '<div class="checkbox t3-form-field-eval-null-checkbox">';
+            $fullElement[] =     '<label>';
+            $fullElement[] =         '<input type="hidden"' . $nullControlNameAttribute . ' value="0" />';
+            $fullElement[] =         '<input type="checkbox"' . $nullControlNameAttribute . ' value="1"' . $checked . ' />';
+            $fullElement[] =         $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.nullCheckbox');
+            $fullElement[] =     '</label>';
+            $fullElement[] = '</div>';
+            $fullElement[] = $mainFieldHtml;
+            $fullElement = implode(LF, $fullElement);
+        } elseif ($this->hasNullCheckboxWithPlaceholder()) {
+            $checked = $itemValue !== null ? ' checked="checked"' : '';
+            $placeholder = $shortenedPlaceholder = $config['placeholder'] ?? '';
+            $disabled = '';
+            $fallbackValue = 0;
+            if (strlen($placeholder) > 0) {
+                $shortenedPlaceholder = GeneralUtility::fixed_lgd_cs($placeholder, 20);
+                if ($placeholder !== $shortenedPlaceholder) {
+                    $overrideLabel = sprintf(
+                        $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
+                        '<span title="' . htmlspecialchars($placeholder) . '">' . htmlspecialchars($shortenedPlaceholder) . '</span>'
+                    );
+                } else {
+                    $overrideLabel = sprintf(
+                        $languageService->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override'),
+                        htmlspecialchars($placeholder)
+                    );
                 }
+            } else {
+                $fallbackValue = 1;
+                $checked = ' checked="checked"';
+                $disabled = ' disabled="disabled"';
+                $overrideLabel = $languageService->sL(
+                    'LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.placeholder.override_not_available'
+                );
             }
+            $fullElement = [];
+            $fullElement[] = '<div class="checkbox t3js-form-field-eval-null-placeholder-checkbox">';
+            $fullElement[] =     '<label>';
+            $fullElement[] =         '<input type="hidden"' . $nullControlNameAttribute . ' value="' . $fallbackValue . '" />';
+            $fullElement[] =         '<input type="checkbox"' . $nullControlNameAttribute . ' value="1"' . $checked . $disabled . ' />';
+            $fullElement[] =         $overrideLabel;
+            $fullElement[] =     '</label>';
+            $fullElement[] = '</div>';
+            $fullElement[] = '<div class="t3js-formengine-placeholder-placeholder">';
+            $fullElement[] =    '<div class="form-control-wrap" style="max-width:' . $width . 'px">';
+            $fullElement[] =        '<input type="text" class="form-control" disabled="disabled" value="' . $shortenedPlaceholder . '" />';
+            $fullElement[] =    '</div>';
+            $fullElement[] = '</div>';
+            $fullElement[] = '<div class="t3js-formengine-placeholder-formfield">';
+            $fullElement[] =    $mainFieldHtml;
+            $fullElement[] = '</div>';
+            $fullElement = implode(LF, $fullElement);
         }
 
-        // Wrap a wizard around the item?
-        $html = $this->renderWizards(
-            [$html],
-            $config['wizards'],
-            $table,
-            $row,
-            $fieldName,
-            $parameterArray,
-            $parameterArray['itemFormElName'],
-            $specConf
-        );
-
-        // Add a wrapper to remain maximum width
-        $width = (int)$this->formMaxWidth($size);
-        $html = '<div class="form-control-wrap"' . ($width ? ' style="max-width: ' . $width . 'px"' : '') . '>' . $html . '</div>';
-        $resultArray['html'] = $html;
+        $resultArray['html'] = '<div class="t3js-formengine-field-item">' . $fieldInformationHtml . $fullElement . '</div>';
         return $resultArray;
     }
+
+    /**
+     * @return LanguageService
+     */
+    protected function getLanguageService()
+    {
+        return $GLOBALS['LANG'];
+    }
 }