| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ilateral\SilverStripe\ModelAdminPlus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Forms\FormField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use TractorCow\AutoComplete\AutoCompleteField as TractorCowAutoCompleteField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Custom autocomplete that loads extra GraphQL data | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | class AutoCompleteField extends TractorCowAutoCompleteField | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     //protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_CUSTOM; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     public function getSchemaDataDefaults() | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         $data = parent::getSchemaDataDefaults(); | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         $data['placeholder'] = 'Search on ' . implode(' or ', $this->getSourceFields()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         if (!isset($data['attributes'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |             $data['attributes'] = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         $data['attributes'] = array_merge( | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |             $data['attributes'], | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |                 'data-source' => $this->getSuggestURL(), | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |                 'data-min-length' => $this->getMinSearchLength(), | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |                 'data-require-selection' => $this->getRequireSelection(), | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |                 'data-pop-separate' => $this->getPopulateSeparately(), | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |                 'data-clear-input' => $this->getClearInput() | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             ] | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         // Override the value so we start with a clear search form (depending on configuration). | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $data['value'] = ($this->getPopulateSeparately() ? null : $this->Value()); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         return $data; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function getSchemaStateDefaults() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $state = parent::getSchemaStateDefaults(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $state['data']['autocomplete'] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             'source' => $this->getSuggestURL(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             'min-length' => $this->getMinSearchLength(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             'require-selection' => $this->getRequireSelection(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             'pop-separate' => $this->getPopulateSeparately(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             'clear-input' => $this->getClearInput() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |         return $state; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |  |