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