[BUGFIX] Allow usage of uid in slug generation via Ajax 70/62170/3
authorJohannes Schlier <johannes.schlier@b13.com>
Thu, 31 Oct 2019 12:04:37 +0000 (13:04 +0100)
committerAndreas Fernandez <a.fernandez@scripting-base.de>
Sat, 9 Nov 2019 16:36:09 +0000 (17:36 +0100)
This commit adds an option to the slug input type which allows it
to use the uid of a record when the update slug button is pressed
in the FormEngine.

Resolves: #89560
Releases: master, 9.5
Change-Id: Id645714f1b26e20e511022d1c74ea793964ffa19
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/62170
Reviewed-by: Daniel Sattler <sattler@b13.de>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Wolfgang Klinger <wolfgang@wazum.com>
Reviewed-by: Susanne Moog <look@susi.dev>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Oliver Bartsch <bo@cedev.de>
Tested-by: Susanne Moog <look@susi.dev>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Build/Sources/TypeScript/backend/Resources/Public/TypeScript/FormEngine/Element/SlugElement.ts
typo3/sysext/backend/Classes/Form/Element/InputSlugElement.php
typo3/sysext/backend/Resources/Public/JavaScript/FormEngine/Element/SlugElement.js

index f619ff5..e2ae2f4 100644 (file)
@@ -25,6 +25,7 @@ interface FieldOptions {
   signature: string;
   command: string;
   parentPageId: number;
+  includeUidInValues: boolean;
 }
 
 interface Response {
@@ -151,6 +152,9 @@ class SlugElement {
       $.each(this.getAvailableFieldsForProposalGeneration(), (fieldName: string, field: string): void => {
         input[fieldName] = $('[data-formengine-input-name="' + field + '"]').val();
       });
+      if (this.options.includeUidInValues === true) {
+        input.uid = this.options.recordId.toString();
+      }
     } else {
       input.manual = this.$inputField.val();
     }
index 865678f..c486bda 100644 (file)
@@ -166,11 +166,16 @@ class InputSlugElement extends AbstractFormElement
 
         [$commonElementPrefix] = GeneralUtility::revExplode('[', $parameterArray['itemFormElName'], 2);
         $validInputNamesToListenTo = [];
+        $includeUidInValues = false;
         foreach ($config['generatorOptions']['fields'] ?? [] as $fieldNameParts) {
             if (is_string($fieldNameParts)) {
                 $fieldNameParts = GeneralUtility::trimExplode(',', $fieldNameParts);
             }
             foreach ($fieldNameParts as $listenerFieldName) {
+                if ($listenerFieldName === 'uid') {
+                    $includeUidInValues = true;
+                    continue;
+                }
                 $validInputNamesToListenTo[$listenerFieldName] = $commonElementPrefix . '[' . htmlspecialchars($listenerFieldName) . ']';
             }
         }
@@ -202,6 +207,7 @@ class InputSlugElement extends AbstractFormElement
             'signature' => $signature,
             'command' => $this->data['command'],
             'parentPageId' => $parentPageId,
+            'includeUidInValues' => $includeUidInValues,
         ];
         $resultArray['requireJsModules'][] = ['TYPO3/CMS/Backend/FormEngine/Element/SlugElement' => '
             function(SlugElement) {
index c6c4c5b..67b0746 100644 (file)
@@ -10,4 +10,4 @@
  *
  * The TYPO3 project - inspiring people to share!
  */
-define(["require","exports","jquery"],function(e,l,i){"use strict";var t,s;!function(e){e.toggleButton=".t3js-form-field-slug-toggle",e.recreateButton=".t3js-form-field-slug-recreate",e.inputField=".t3js-form-field-slug-input",e.readOnlyField=".t3js-form-field-slug-readonly",e.hiddenField=".t3js-form-field-slug-hidden"}(t||(t={})),function(e){e.AUTO="auto",e.RECREATE="recreate",e.MANUAL="manual"}(s||(s={}));return class{constructor(e,l){this.options=null,this.$fullElement=null,this.manuallyChanged=!1,this.$readOnlyField=null,this.$inputField=null,this.$hiddenField=null,this.xhr=null,this.fieldsToListenOn={},this.options=l,this.fieldsToListenOn=this.options.listenerFieldNames||{},i(()=>{this.$fullElement=i(e),this.$inputField=this.$fullElement.find(t.inputField),this.$readOnlyField=this.$fullElement.find(t.readOnlyField),this.$hiddenField=this.$fullElement.find(t.hiddenField),this.registerEvents()})}registerEvents(){const e=Object.keys(this.getAvailableFieldsForProposalGeneration()).map(e=>this.fieldsToListenOn[e]);e.length>0?("new"===this.options.command&&i(this.$fullElement).on("keyup",e.join(","),()=>{this.manuallyChanged||this.sendSlugProposal(s.AUTO)}),i(this.$fullElement).on("click",t.recreateButton,e=>{e.preventDefault(),this.$readOnlyField.hasClass("hidden")&&(this.$readOnlyField.toggleClass("hidden",!1),this.$inputField.toggleClass("hidden",!0)),this.sendSlugProposal(s.RECREATE)})):i(this.$fullElement).find(t.recreateButton).addClass("disabled").prop("disabled",!0),i(this.$inputField).on("keyup",()=>{this.manuallyChanged=!0,this.sendSlugProposal(s.MANUAL)}),i(this.$fullElement).on("click",t.toggleButton,e=>{e.preventDefault();const l=this.$readOnlyField.hasClass("hidden");this.$readOnlyField.toggleClass("hidden",!l),this.$inputField.toggleClass("hidden",l),l?(this.$inputField.val()!==this.$readOnlyField.val()?this.$readOnlyField.val(this.$inputField.val()):(this.manuallyChanged=!1,this.$fullElement.find(".t3js-form-proposal-accepted").addClass("hidden"),this.$fullElement.find(".t3js-form-proposal-different").addClass("hidden")),this.$hiddenField.val(this.$readOnlyField.val())):this.$hiddenField.val(this.$inputField.val())})}sendSlugProposal(e){const l={};e===s.AUTO||e===s.RECREATE?i.each(this.getAvailableFieldsForProposalGeneration(),(e,t)=>{l[e]=i('[data-formengine-input-name="'+t+'"]').val()}):l.manual=this.$inputField.val(),null!==this.xhr&&4!==this.xhr.readyState&&this.xhr.abort(),this.xhr=i.post(TYPO3.settings.ajaxUrls.record_slug_suggest,{values:l,mode:e,tableName:this.options.tableName,pageId:this.options.pageId,parentPageId:this.options.parentPageId,recordId:this.options.recordId,language:this.options.language,fieldName:this.options.fieldName,command:this.options.command,signature:this.options.signature},l=>{l.hasConflicts?(this.$fullElement.find(".t3js-form-proposal-accepted").addClass("hidden"),this.$fullElement.find(".t3js-form-proposal-different").removeClass("hidden").find("span").text(l.proposal)):(this.$fullElement.find(".t3js-form-proposal-accepted").removeClass("hidden").find("span").text(l.proposal),this.$fullElement.find(".t3js-form-proposal-different").addClass("hidden")),this.$hiddenField.val()!==l.proposal&&this.$fullElement.find("input").trigger("change"),e===s.AUTO||e===s.RECREATE?(this.$readOnlyField.val(l.proposal),this.$hiddenField.val(l.proposal),this.$inputField.val(l.proposal)):this.$hiddenField.val(l.proposal)},"json")}getAvailableFieldsForProposalGeneration(){const e={};return i.each(this.fieldsToListenOn,(l,t)=>{i('[data-formengine-input-name="'+t+'"]').length>0&&(e[l]=t)}),e}}});
\ No newline at end of file
+define(["require","exports","jquery"],function(e,i,l){"use strict";var t,s;!function(e){e.toggleButton=".t3js-form-field-slug-toggle",e.recreateButton=".t3js-form-field-slug-recreate",e.inputField=".t3js-form-field-slug-input",e.readOnlyField=".t3js-form-field-slug-readonly",e.hiddenField=".t3js-form-field-slug-hidden"}(t||(t={})),function(e){e.AUTO="auto",e.RECREATE="recreate",e.MANUAL="manual"}(s||(s={}));return class{constructor(e,i){this.options=null,this.$fullElement=null,this.manuallyChanged=!1,this.$readOnlyField=null,this.$inputField=null,this.$hiddenField=null,this.xhr=null,this.fieldsToListenOn={},this.options=i,this.fieldsToListenOn=this.options.listenerFieldNames||{},l(()=>{this.$fullElement=l(e),this.$inputField=this.$fullElement.find(t.inputField),this.$readOnlyField=this.$fullElement.find(t.readOnlyField),this.$hiddenField=this.$fullElement.find(t.hiddenField),this.registerEvents()})}registerEvents(){const e=Object.keys(this.getAvailableFieldsForProposalGeneration()).map(e=>this.fieldsToListenOn[e]);e.length>0?("new"===this.options.command&&l(this.$fullElement).on("keyup",e.join(","),()=>{this.manuallyChanged||this.sendSlugProposal(s.AUTO)}),l(this.$fullElement).on("click",t.recreateButton,e=>{e.preventDefault(),this.$readOnlyField.hasClass("hidden")&&(this.$readOnlyField.toggleClass("hidden",!1),this.$inputField.toggleClass("hidden",!0)),this.sendSlugProposal(s.RECREATE)})):l(this.$fullElement).find(t.recreateButton).addClass("disabled").prop("disabled",!0),l(this.$inputField).on("keyup",()=>{this.manuallyChanged=!0,this.sendSlugProposal(s.MANUAL)}),l(this.$fullElement).on("click",t.toggleButton,e=>{e.preventDefault();const i=this.$readOnlyField.hasClass("hidden");this.$readOnlyField.toggleClass("hidden",!i),this.$inputField.toggleClass("hidden",i),i?(this.$inputField.val()!==this.$readOnlyField.val()?this.$readOnlyField.val(this.$inputField.val()):(this.manuallyChanged=!1,this.$fullElement.find(".t3js-form-proposal-accepted").addClass("hidden"),this.$fullElement.find(".t3js-form-proposal-different").addClass("hidden")),this.$hiddenField.val(this.$readOnlyField.val())):this.$hiddenField.val(this.$inputField.val())})}sendSlugProposal(e){const i={};e===s.AUTO||e===s.RECREATE?(l.each(this.getAvailableFieldsForProposalGeneration(),(e,t)=>{i[e]=l('[data-formengine-input-name="'+t+'"]').val()}),!0===this.options.includeUidInValues&&(i.uid=this.options.recordId.toString())):i.manual=this.$inputField.val(),null!==this.xhr&&4!==this.xhr.readyState&&this.xhr.abort(),this.xhr=l.post(TYPO3.settings.ajaxUrls.record_slug_suggest,{values:i,mode:e,tableName:this.options.tableName,pageId:this.options.pageId,parentPageId:this.options.parentPageId,recordId:this.options.recordId,language:this.options.language,fieldName:this.options.fieldName,command:this.options.command,signature:this.options.signature},i=>{i.hasConflicts?(this.$fullElement.find(".t3js-form-proposal-accepted").addClass("hidden"),this.$fullElement.find(".t3js-form-proposal-different").removeClass("hidden").find("span").text(i.proposal)):(this.$fullElement.find(".t3js-form-proposal-accepted").removeClass("hidden").find("span").text(i.proposal),this.$fullElement.find(".t3js-form-proposal-different").addClass("hidden")),this.$hiddenField.val()!==i.proposal&&this.$fullElement.find("input").trigger("change"),e===s.AUTO||e===s.RECREATE?(this.$readOnlyField.val(i.proposal),this.$hiddenField.val(i.proposal),this.$inputField.val(i.proposal)):this.$hiddenField.val(i.proposal)},"json")}getAvailableFieldsForProposalGeneration(){const e={};return l.each(this.fieldsToListenOn,(i,t)=>{l('[data-formengine-input-name="'+t+'"]').length>0&&(e[i]=t)}),e}}});
\ No newline at end of file