@@ -34,88 +34,150 @@ protected static function resolveResourceModel(string $tableName): ?object
3434
3535 protected static function addValuesToInput (mixed $ input , mixed $ field , string $ type , string $ method ): mixed
3636 {
37+ // Ensure field config is properly initialized
38+ if (!static ::ensureFieldConfig ($ field , $ type )) {
39+ return $ input ;
40+ }
41+
3742 $ allOptions = [];
3843
3944 // Handle relationship options
40- if (isset ($ field ->config [$ type ]) &&
41- (is_string ($ field ->config [$ type ]) && $ field ->config [$ type ] === 'relationship ' ) ||
42- (is_array ($ field ->config [$ type ]) && in_array ('relationship ' , $ field ->config [$ type ]))) {
45+ if (static ::shouldHandleRelationshipOptions ($ field , $ type )) {
46+ $ relationshipOptions = static ::buildRelationshipOptions ($ field );
47+ $ allOptions = static ::mergeRelationshipOptions ($ allOptions , $ relationshipOptions , $ field , $ type );
48+ }
4349
44- $ relationshipOptions = [];
50+ // Handle array options
51+ if (static ::shouldHandleArrayOptions ($ field , $ type )) {
52+ $ allOptions = static ::mergeArrayOptions ($ allOptions , $ field , $ type );
53+ }
4554
46- foreach ( $ field -> config [ ' relations ' ] ?? [] as $ relation ) {
47- if (! isset ( $ relation [ ' resource ' ] )) {
48- continue ;
49- }
55+ // Apply all merged options to the input
56+ if (!empty ( $ allOptions )) {
57+ $ input -> $ method ( $ allOptions ) ;
58+ }
5059
51- $ model = static ::resolveResourceModel ($ relation ['resource ' ]);
60+ return $ input ;
61+ }
5262
53- if (! $ model ) {
54- continue ;
55- }
63+ protected static function ensureFieldConfig (mixed $ field , string $ type ): bool
64+ {
65+ // Ensure field config exists and is an array
66+ if (!isset ($ field ->config ) || !is_array ($ field ->config )) {
67+ return false ;
68+ }
5669
57- $ query = $ model ::query ();
70+ // Ensure the type key exists in the config to prevent undefined array key errors
71+ if (!array_key_exists ($ type , $ field ->config )) {
72+ $ config = $ field ->config ?? [];
73+ $ config [$ type ] = null ;
74+ $ field ->config = $ config ;
75+ }
5876
59- // Apply filters if they exist
60- if (isset ($ relation ['relationValue_filters ' ])) {
61- foreach ($ relation ['relationValue_filters ' ] as $ filter ) {
62- if (isset ($ filter ['column ' ], $ filter ['operator ' ], $ filter ['value ' ])) {
63- $ query ->where ($ filter ['column ' ], $ filter ['operator ' ], $ filter ['value ' ]);
64- }
65- }
66- }
77+ return true ;
78+ }
6779
68- $ results = $ query ->get ();
80+ protected static function shouldHandleRelationshipOptions (mixed $ field , string $ type ): bool
81+ {
82+ // Ensure $type is a string to prevent array key errors
83+ if (!is_string ($ type )) {
84+ return false ;
85+ }
86+
87+ return isset ($ field ->config [$ type ]) && $ field ->config [$ type ] !== null &&
88+ (is_string ($ field ->config [$ type ]) && $ field ->config [$ type ] === 'relationship ' ) ||
89+ (is_array ($ field ->config [$ type ]) && in_array ('relationship ' , $ field ->config [$ type ]));
90+ }
6991
70- if ($ results ->isEmpty ()) {
71- continue ;
72- }
92+ protected static function shouldHandleArrayOptions (mixed $ field , string $ type ): bool
93+ {
94+ // Ensure $type is a string to prevent array key errors
95+ if (!is_string ($ type )) {
96+ return false ;
97+ }
98+
99+ return isset ($ field ->config [$ type ]) && $ field ->config [$ type ] !== null &&
100+ (is_string ($ field ->config [$ type ]) && $ field ->config [$ type ] === 'array ' ) ||
101+ (is_array ($ field ->config [$ type ]) && in_array ('array ' , $ field ->config [$ type ]));
102+ }
73103
74- $ opts = $ results ->pluck ($ relation ['relationValue ' ] ?? 'name ' , $ relation ['relationKey ' ])->toArray ();
104+ protected static function buildRelationshipOptions (mixed $ field ): array
105+ {
106+ $ relationshipOptions = [];
75107
76- if (count ($ opts ) === 0 ) {
77- continue ;
78- }
108+ foreach ($ field ->config ['relations ' ] ?? [] as $ relation ) {
109+ if (!isset ($ relation ['resource ' ])) {
110+ continue ;
111+ }
112+
113+ $ model = static ::resolveResourceModel ($ relation ['resource ' ]);
79114
80- // Group by resource name
81- $ resourceName = Str::title ($ relation ['resource ' ]);
82- $ relationshipOptions [$ resourceName ] = $ opts ;
115+ if (!$ model ) {
116+ continue ;
83117 }
84118
85- if (! empty ( $ relationshipOptions )) {
86- // If both types are selected, group relationship options by resource
87- if ( isset ( $ field -> config [ $ type ]) &&
88- ( is_array ( $ field -> config [ $ type ]) && in_array ( ' array ' , $ field -> config [ $ type ]) )) {
89- $ allOptions = array_merge ( $ allOptions , $ relationshipOptions );
90- } else {
91- // For single relationship type, merge all options without grouping
92- $ allOptions = array_merge ( $ allOptions , ... array_values ( $ relationshipOptions ));
119+ $ query = $ model :: query ();
120+
121+ // Apply filters if they exist
122+ if ( isset ( $ relation [ ' relationValue_filters ' ] )) {
123+ foreach ( $ relation [ ' relationValue_filters ' ] as $ filter ) {
124+ if ( isset ( $ filter [ ' column ' ], $ filter [ ' operator ' ], $ filter [ ' value ' ])) {
125+ $ query -> where ( $ filter [ ' column ' ], $ filter [ ' operator ' ], $ filter [ ' value ' ]);
126+ }
93127 }
94128 }
129+
130+ $ results = $ query ->get ();
131+
132+ if ($ results ->isEmpty ()) {
133+ continue ;
134+ }
135+
136+ $ opts = $ results ->pluck ($ relation ['relationValue ' ] ?? 'name ' , $ relation ['relationKey ' ])->toArray ();
137+
138+ if (count ($ opts ) === 0 ) {
139+ continue ;
140+ }
141+
142+ // Group by resource name
143+ $ resourceName = Str::title ($ relation ['resource ' ]);
144+ $ relationshipOptions [$ resourceName ] = $ opts ;
95145 }
96146
97- // Handle array options
98- if (isset ($ field ->config [$ type ]) &&
99- (is_string ($ field ->config [$ type ]) && $ field ->config [$ type ] === 'array ' ) ||
147+ return $ relationshipOptions ;
148+ }
149+
150+ protected static function mergeRelationshipOptions (array $ allOptions , array $ relationshipOptions , mixed $ field , string $ type ): array
151+ {
152+ if (empty ($ relationshipOptions )) {
153+ return $ allOptions ;
154+ }
155+
156+ // If both types are selected, group relationship options by resource
157+ if (isset ($ field ->config [$ type ]) && $ field ->config [$ type ] !== null &&
100158 (is_array ($ field ->config [$ type ]) && in_array ('array ' , $ field ->config [$ type ]))) {
159+ return array_merge ($ allOptions , $ relationshipOptions );
160+ } else {
161+ // For single relationship type, merge all options without grouping
162+ return array_merge ($ allOptions , ...array_values ($ relationshipOptions ));
163+ }
164+ }
101165
102- if (isset ($ field ->config ['options ' ]) && is_array ($ field ->config ['options ' ])) {
103- // If both types are selected, group array options
104- if (isset ($ field ->config [$ type ]) &&
105- (is_array ($ field ->config [$ type ]) && in_array ('relationship ' , $ field ->config [$ type ]))) {
106- $ allOptions [__ ('Custom Options ' )] = $ field ->config ['options ' ];
107- } else {
108- $ allOptions = array_merge ($ allOptions , $ field ->config ['options ' ]);
109- }
110- }
166+ protected static function mergeArrayOptions (array $ allOptions , mixed $ field , string $ type ): array
167+ {
168+ if (!isset ($ field ->config ['options ' ]) || !is_array ($ field ->config ['options ' ])) {
169+ return $ allOptions ;
111170 }
112171
113- // Apply all merged options to the input
114- if (! empty ($ allOptions )) {
115- $ input ->$ method ($ allOptions );
172+ // If both types are selected, group array options
173+ if (isset ($ field ->config [$ type ]) && $ field ->config [$ type ] !== null &&
174+ (is_array ($ field ->config [$ type ]) && in_array ('relationship ' , $ field ->config [$ type ]))) {
175+ $ allOptions [__ ('Custom Options ' )] = $ field ->config ['options ' ];
176+ } else {
177+ $ allOptions = array_merge ($ allOptions , $ field ->config ['options ' ]);
116178 }
117179
118- return $ input ;
180+ return $ allOptions ;
119181 }
120182
121183 protected static function getSelectableValuesConfig (): array
0 commit comments