@@ -86,82 +86,82 @@ export const removeReferenceFields = async function (
8686 flag = { supressed : false } ,
8787 stackAPIClient : any ,
8888) : Promise < boolean | void > {
89- if ( schema && schema . length ) {
90- for ( let i = 0 ; i < schema . length ; i ++ ) {
91- if ( schema [ i ] . data_type === 'group' ) {
92- await removeReferenceFields ( schema [ i ] . schema , flag , stackAPIClient ) ;
93- } else if ( schema [ i ] . data_type === 'blocks' ) {
94- for ( var block in schema [ i ] . blocks ) {
95- await removeReferenceFields ( schema [ i ] . blocks [ block ] . schema , flag , stackAPIClient ) ;
96- }
97- } else if ( schema [ i ] . data_type === 'reference' ) {
98- flag . supressed = true ;
99- // Check if content-type exists
100- // If exists, then no change should be required.
101- let isContentTypeError = false ;
102- for ( let j = 0 ; j < schema [ i ] . reference_to . length ; j ++ ) {
103- try {
104- await stackAPIClient . contentType ( schema [ i ] . reference_to [ j ] ) . fetch ( ) ;
105- } catch ( error ) {
106- // Else warn and modify the schema object.
107- isContentTypeError = true ;
108- console . warn ( `Content-type ${ schema [ i ] . reference_to [ j ] } does not exist. Removing the field from schema` ) ;
89+ if ( schema ?. length ) {
90+ for ( let i = 0 ; i < schema . length ; i ++ ) {
91+ if ( schema [ i ] . data_type === 'group' ) {
92+ await removeReferenceFields ( schema [ i ] . schema , flag , stackAPIClient ) ;
93+ } else if ( schema [ i ] . data_type === 'blocks' ) {
94+ for ( var block in schema [ i ] . blocks ) {
95+ await removeReferenceFields ( schema [ i ] . blocks [ block ] . schema , flag , stackAPIClient ) ;
10996 }
110- }
111- if ( isContentTypeError ) {
112- schema . splice ( i , 1 ) ;
113- -- i ;
114- if ( schema . length < 1 ) {
115- schema . push ( {
116- data_type : 'text' ,
117- display_name : 'dummyTest' ,
118- uid : 'dummy_test' ,
119- field_metadata : {
120- description : '' ,
121- default_value : '' ,
122- version : 3 ,
123- } ,
124- format : '' ,
125- error_messages : {
97+ } else if ( schema [ i ] . data_type === 'reference' ) {
98+ flag . supressed = true ;
99+ // Check if content-type exists
100+ // If exists, then no change should be required.
101+ let isContentTypeError = false ;
102+ for ( let j = 0 ; j < schema [ i ] . reference_to . length ; j ++ ) {
103+ try {
104+ await stackAPIClient . contentType ( schema [ i ] . reference_to [ j ] ) . fetch ( ) ;
105+ } catch ( error ) {
106+ // Else warn and modify the schema object.
107+ isContentTypeError = true ;
108+ console . warn ( `Content-type ${ schema [ i ] . reference_to [ j ] } does not exist. Removing the field from schema` ) ;
109+ }
110+ }
111+ if ( isContentTypeError ) {
112+ schema . splice ( i , 1 ) ;
113+ -- i ;
114+ if ( schema . length < 1 ) {
115+ schema . push ( {
116+ data_type : 'text' ,
117+ display_name : 'dummyTest' ,
118+ uid : 'dummy_test' ,
119+ field_metadata : {
120+ description : '' ,
121+ default_value : '' ,
122+ version : 3 ,
123+ } ,
126124 format : '' ,
127- } ,
128- multiple : false ,
129- mandatory : false ,
130- unique : false ,
131- non_localizable : false ,
132- } ) ;
125+ error_messages : {
126+ format : '' ,
127+ } ,
128+ multiple : false ,
129+ mandatory : false ,
130+ unique : false ,
131+ non_localizable : false ,
132+ } ) ;
133+ }
133134 }
135+ } else if (
136+ // handling entry references in json rte
137+ schema [ i ] . data_type === 'json' &&
138+ schema [ i ] . field_metadata . allow_json_rte &&
139+ schema [ i ] . field_metadata . embed_entry &&
140+ schema [ i ] . reference_to . length > 1
141+ ) {
142+ flag . supressed = true ;
143+ schema [ i ] . reference_to = [ 'sys_assets' ] ;
144+ } else if (
145+ // handling entry references in json rte
146+ schema [ i ] . data_type === 'json' &&
147+ schema [ i ] ?. field_metadata ?. rich_text_type &&
148+ schema [ i ] ?. field_metadata ?. embed_entry &&
149+ schema [ i ] ?. reference_to ?. length > 1
150+ ) {
151+ flag . supressed = true ;
152+ schema [ i ] . reference_to = [ 'sys_assets' ] ;
153+ } else if (
154+ // handling entry references in rte
155+ schema [ i ] . data_type === 'text' &&
156+ schema [ i ] ?. field_metadata ?. rich_text_type &&
157+ schema [ i ] ?. field_metadata ?. embed_entry &&
158+ schema [ i ] ?. reference_to ?. length >= 1
159+ ) {
160+ flag . supressed = true ;
161+ schema [ i ] . reference_to = [ 'sys_assets' ] ;
134162 }
135- } else if (
136- // handling entry references in json rte
137- schema [ i ] . data_type === 'json' &&
138- schema [ i ] . field_metadata . allow_json_rte &&
139- schema [ i ] . field_metadata . embed_entry &&
140- schema [ i ] . reference_to . length > 1
141- ) {
142- flag . supressed = true ;
143- schema [ i ] . reference_to = [ 'sys_assets' ] ;
144- } else if (
145- // handling entry references in json rte
146- schema [ i ] . data_type === 'json' &&
147- schema [ i ] ?. field_metadata ?. rich_text_type &&
148- schema [ i ] ?. field_metadata ?. embed_entry &&
149- schema [ i ] ?. reference_to ?. length > 1
150- ) {
151- flag . supressed = true ;
152- schema [ i ] . reference_to = [ 'sys_assets' ] ;
153- } else if (
154- // handling entry references in rte
155- schema [ i ] . data_type === 'text' &&
156- schema [ i ] ?. field_metadata ?. rich_text_type &&
157- schema [ i ] ?. field_metadata ?. embed_entry &&
158- schema [ i ] ?. reference_to ?. length >= 1
159- ) {
160- flag . supressed = true ;
161- schema [ i ] . reference_to = [ 'sys_assets' ] ;
162163 }
163164 }
164- }
165165} ;
166166
167167export const updateFieldRules = function ( contentType : any ) {
0 commit comments