[TASK] sys_file/sys_file_reference override strategy 31/21931/9
authorAndreas Wolf <andreas.wolf@typo3.org>
Fri, 5 Jul 2013 13:15:56 +0000 (15:15 +0200)
committerSteffen Ritter <info@rs-websystems.de>
Wed, 28 Aug 2013 12:24:35 +0000 (14:24 +0200)
Until now, the sys_file_reference records use the regular "null value"
checkbox to switch between the value from sys_file and a custom value
the user can supply. This is however confusing for users, as there is no
explanation what the checkbox really does and also the placeholder text
is not removed when checking the checkbox (but an empty string is stored
in the database and thus used for output afterwards).

This commit changes the appearance to have a checkbox on top and toggle
between a classical read-only field and a real input field.

Change-Id: I2370b17c2cdf475aa6742cbca7cd12b6c8558130
Resolves: #49685
Releases: 6.2, 6.1, 6.0
Reviewed-on: https://review.typo3.org/21931
Reviewed-by: Steffen Ritter
Tested-by: Steffen Ritter
typo3/sysext/backend/Classes/Form/FormEngine.php
typo3/sysext/backend/Resources/Public/JavaScript/jsfunc.tbe_editor.js
typo3/sysext/core/Configuration/TCA/sys_file_reference.php
typo3/sysext/lang/locallang_core.xlf

index e8a3798..37ab163 100644 (file)
@@ -42,7 +42,7 @@ use TYPO3\CMS\Core\Utility\MathUtility;
  * @author Kasper Skårhøj <kasperYYYY@typo3.com>
  * @coauthor René Fritz <r.fritz@colorcube.de>
  */
-class FormEngine {
+class  FormEngine {
 
        // variables not commented yet.... (do so...)
        /**
@@ -1012,6 +1012,7 @@ class FormEngine {
                // Get the TCA configuration for the current field:
                $PA['fieldConf'] = $GLOBALS['TCA'][$table]['columns'][$field];
                $PA['fieldConf']['config']['form_type'] = $PA['fieldConf']['config']['form_type'] ? $PA['fieldConf']['config']['form_type'] : $PA['fieldConf']['config']['type'];
+
                // Using "form_type" locally in this script
                $skipThisField = $this->inline->skipField($table, $field, $row, $PA['fieldConf']['config']);
 
@@ -1022,7 +1023,6 @@ class FormEngine {
                        $elementConditionMatcher = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Form\\ElementConditionMatcher');
                        $displayConditionResult = $elementConditionMatcher->match($PA['fieldConf']['displayCond'], $row);
                }
-
                // Check if this field is configured and editable (according to excludefields + other configuration)
                if (
                        is_array($PA['fieldConf'])
@@ -1129,6 +1129,29 @@ class FormEngine {
                                                $lTTS_url = $this->backPath . 'alt_doc.php?edit[' . $table . '][' . $row['uid'] . ']=edit&columnsOnly=' . $field . '&returnUrl=' . rawurlencode($this->thisReturnUrl());
                                                $label = '<a href="' . htmlspecialchars($lTTS_url) . '">' . $label . '</a>';
                                        }
+
+                                       if (isset($PA['fieldConf']['config']['mode']) && $PA['fieldConf']['config']['mode'] == 'useOrOverridePlaceholder') {
+                                               $placeholder = $this->getPlaceholderValue($table, $field, $PA['fieldConf']['config'], $row);
+                                               $onChange = htmlspecialchars(
+                                                       'typo3form.fieldTogglePlaceholder(\'' . $PA['itemFormElName'] . '\', !this.checked)'
+                                               );
+
+                                               $isNull = ($PA['itemFormElValue'] === NULL);
+                                               $checked = (($isNull || $this->isNewRecord($table, $row)) ? '' : ' checked="checked"');
+
+                                               $this->additionalJS_post[] = 'typo3form.fieldTogglePlaceholder(\''
+                                                       . $PA['itemFormElName'] . '\', ' . ($checked ? 'false' : 'true') . ');';
+
+                                               $item = '<div class="t3-form-field-placeholder-override">'
+                                               . '<span class="t3-tceforms-placeholder-override-checkbox">' .
+                                                       '<input type="hidden" name="' . $PA['itemFormElNameActive'] . '" value="0" />' .
+                                                       '<input type="checkbox" name="' . $PA['itemFormElNameActive'] . '" value="1" id="tce-forms-textfield-use-override-' . $field . '-' . $row['uid'] . '" onchange="' . $onChange . '"' . $checked . ' /> <label for="tce-forms-textfield-use-override-' . $field . '-' . $row['uid'] . '">' . sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.placeholder.override'), $placeholder) . '</label>' .
+                                               '</span>'
+                                               . '<div class="t3-form-placeholder-placeholder">' . $this->getSingleField_typeNone_render($PA['fieldConf']['config'], $placeholder) . '</div>'
+                                               . '<div class="t3-form-placeholder-formfield">' . $item . '</div>'
+                                               . '</div>';
+                                       }
+
                                        // Wrap the label with help text
                                        $PA['label'] = ($label = BackendUtility::wrapInHelp($table, $field, $label));
                                        // Create output value:
@@ -1142,7 +1165,7 @@ class FormEngine {
                                                        'FIELD' => $field,
                                                        'TABLE' => $table,
                                                        'ITEM' => $item,
-                                                       'ITEM_DISABLED' => ($this->isNullValue($table, $field, $row, $PA) ? ' disabled' : ''),
+                                                       'ITEM_DISABLED' => ($this->isDisabledNullValueField($table, $field, $row, $PA) ? ' disabled' : ''),
                                                        'ITEM_NULLVALUE' => $this->renderNullValueWidget($table, $field, $row, $PA),
                                                );
                                                $out = $this->addUserTemplateMarkers($out, $table, $field, $row, $PA);
@@ -1155,7 +1178,7 @@ class FormEngine {
                                                        'ID' => $row['uid'],
                                                        'PAL_LINK_ICON' => $thePalIcon,
                                                        'FIELD' => $field,
-                                                       'ITEM_DISABLED' => ($this->isNullValue($table, $field, $row, $PA) ? ' disabled' : ''),
+                                                       'ITEM_DISABLED' => ($this->isDisabledNullValueField($table, $field, $row, $PA) ? ' disabled' : ''),
                                                        'ITEM_NULLVALUE' => $this->renderNullValueWidget($table, $field, $row, $PA),
                                                );
                                                $out = $this->addUserTemplateMarkers($out, $table, $field, $row, $PA);
@@ -1390,6 +1413,7 @@ function ' . $evalData . '(value) {
                $altItem .= '<input type="hidden" name="' . $PA['itemFormElName'] . '" value="' . htmlspecialchars($PA['itemFormElValue']) . '" />';
                // Wrap a wizard around the item?
                $item = $this->renderWizards(array($item, $altItem), $config['wizards'], $table, $row, $field, $PA, $PA['itemFormElName'] . '_hr', $specConf);
+
                return $item;
        }
 
@@ -1407,7 +1431,7 @@ function ' . $evalData . '(value) {
                $widget = '';
 
                $config = $PA['fieldConf']['config'];
-               if (!empty($config['eval']) && GeneralUtility::inList($config['eval'], 'null')) {
+               if (!empty($config['eval']) && GeneralUtility::inList($config['eval'], 'null') && (empty($config['mode']) || $config['mode'] !== 'useOrOverridePlaceholder')) {
                        $isNull = ($PA['itemFormElValue'] === NULL);
 
                        $checked = ($isNull ? '' : ' checked="checked"');
@@ -1434,11 +1458,14 @@ function ' . $evalData . '(value) {
         * @param array $PA Parameters array with rendering instructions
         * @return boolean
         */
-       protected function isNullValue($table, $field, array $row, array $PA) {
+       protected function isDisabledNullValueField($table, $field, array $row, array $PA) {
                $result = FALSE;
 
                $config = $PA['fieldConf']['config'];
-               if ($PA['itemFormElValue'] === NULL && !empty($config['eval']) && GeneralUtility::inList($config['eval'], 'null')) {
+               if ($PA['itemFormElValue'] === NULL && !empty($config['eval'])
+                       && GeneralUtility::inList($config['eval'], 'null')
+                       && (empty($config['mode']) || $config['mode'] !== 'useOrOverridePlaceholder')) {
+
                        $result = TRUE;
                }
 
@@ -5918,6 +5945,17 @@ function ' . $evalData . '(value) {
        }
 
        /**
+        * Returns TRUE if the given $row is new (i.e. has not been saved to the database)
+        *
+        * @param string $table
+        * @param array $row
+        * @return bool
+        */
+       protected function isNewRecord($table, $row) {
+               return !is_numeric($row['uid']);
+       }
+
+       /**
         * Return record path (visually formatted, using BackendUtility::getRecordPath() )
         *
         * @param string $table Table name
@@ -6308,7 +6346,7 @@ function ' . $evalData . '(value) {
        }
 
        /**
-        * Determine and get the value for the placeholder and return the placeholder attribute
+        * Return the placeholder attribute for an input field.
         *
         * @param string $table
         * @param string $field
@@ -6317,6 +6355,27 @@ function ' . $evalData . '(value) {
         * @return string
         */
        protected function getPlaceholderAttribute($table, $field, array $config, array $row) {
+               if (isset($config['mode']) && $config['mode'] === 'useOrOverridePlaceholder') {
+                       return '';
+               }
+
+               $value = $this->getPlaceholderValue($table, $field, $config, $row);
+
+               // Cleanup the string and support 'LLL:'
+               $value = htmlspecialchars(trim($this->sL($value)));
+               return empty($value) ? '' : ' placeholder="' . $value . '" ';
+       }
+
+       /**
+        * Determine and get the value for the placeholder for an input field.
+        *
+        * @param string $table
+        * @param string $field
+        * @param array $config
+        * @param array $row
+        * @return string
+        */
+       protected function getPlaceholderValue($table, $field, array $config, array $row) {
                $value = trim($config['placeholder']);
                if (!$value) {
                        return '';
@@ -6346,9 +6405,8 @@ function ' . $evalData . '(value) {
                                }
                        }
                }
-               // Cleanup the string and support 'LLL:'
-               $value = htmlspecialchars(trim($this->sL($value)));
-               return empty($value) ? '' : ' placeholder="' . $value . '" ';
+
+               return $value;
        }
 
        /**
index 953d0e5..b57b997 100644 (file)
@@ -605,6 +605,22 @@ var typo3form = {
                        }
                }
        },
+       fieldTogglePlaceholder: function(fieldName, showPlaceholder) {
+               if (!document[TBE_EDITOR.formname][fieldName]) {
+                       return;
+               }
+
+               var formFieldItemWrapper = Element.up(document[TBE_EDITOR.formname][fieldName], '.t3-form-field-item');
+               var placeholder = formFieldItemWrapper.select('.t3-form-placeholder-placeholder')[0];
+               var formField = formFieldItemWrapper.select('.t3-form-placeholder-formfield')[0];
+               if (showPlaceholder) {
+                       placeholder.show();
+                       formField.hide();
+               } else {
+                       placeholder.hide();
+                       formField.show();
+               }
+       },
        fieldSet: function(theField, evallist, is_in, checkbox, checkboxValue) {
                var i;
 
index e43c39f..7d1b700 100644 (file)
@@ -159,9 +159,10 @@ return array(
                        'label' => 'LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.title',
                        'config' => array(
                                'type' => 'input',
-                               'eval' => 'null',
                                'size' => '20',
+                               'eval' => 'null',
                                'placeholder' => '__row|uid_local|title',
+                               'mode' => 'useOrOverridePlaceholder',
                        )
                ),
                'link' => array(
@@ -190,10 +191,11 @@ return array(
                        'label' => 'LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.description',
                        'config' => array(
                                'type' => 'text',
-                               'eval' => 'null',
                                'cols' => '20',
                                'rows' => '5',
+                               'eval' => 'null',
                                'placeholder' => '__row|uid_local|description',
+                               'mode' => 'useOrOverridePlaceholder',
                        )
                ),
                'alternative' => array(
@@ -202,9 +204,10 @@ return array(
                        'label' => 'LLL:EXT:lang/locallang_tca.xlf:sys_file_reference.alternative',
                        'config' => array(
                                'type' => 'input',
-                               'eval' => 'null',
                                'size' => '20',
+                               'eval' => 'null',
                                'placeholder' => '__row|uid_local|alternative',
+                               'mode' => 'useOrOverridePlaceholder',
                        ),
                ),
        ),
@@ -263,4 +266,4 @@ return array(
                )
        )
 );
-?>
\ No newline at end of file
+?>
index 57f9be1..e4821f0 100644 (file)
@@ -313,6 +313,9 @@ Do you want to continue WITHOUT saving?</source>
                        <trans-unit id="labels.uncheckAll" xml:space="preserve">
                                <source>Uncheck all</source>
                        </trans-unit>
+                       <trans-unit id="labels.placeholder.override" xml:space="preserve">
+                               <source>Override "%s"?</source>
+                       </trans-unit>
                        <trans-unit id="labels.changeInOrig" xml:space="preserve">
                                <source>Changed in original translation</source>
                        </trans-unit>