| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dynamic\DynamicBlocks\Block; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Dynamic\DynamicBlocks\Model\PageSectionObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SheaDawson\Blocks\Model\Block; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Forms\GridField\GridField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Forms\GridField\GridFieldDeleteAction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symbiote\GridFieldExtensions\GridFieldOrderableRows; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 | 12 |  |  * Class PageSectionBlock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 | 12 |  |  * @method HasManyList $Sections | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 17 |  | View Code Duplication | class PageSectionBlock extends Block | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 1 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function singular_name() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 1 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         return _t('PageSectionBlock.SINGULARNAME', 'Page Sections Block'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function plural_name() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         return _t('PageSectionBlock.PLURALNAME', 'Page Sections Blocks'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 1 |  |     private static $has_many = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         'Sections' => PageSectionObject::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |      * @return \SilverStripe\Forms\FieldList | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 | 1 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 1 |  |     public function getCMSFields() | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 1 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 1 |  |         $fields = parent::getCMSFields(); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 1 |  |         if ($this->ID) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 1 |  |             // Sections | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 1 |  |             $config = GridFieldConfig_RecordEditor::create(); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             $config->addComponent(new GridFieldOrderableRows('SortOrder')); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 1 |  |             $config->removeComponentsByType('GridFieldAddExistingAutocompleter'); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             $config->removeComponentsByType('GridFieldDeleteAction'); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             $config->addComponent(new GridFieldDeleteAction(false)); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             $sectionsField = GridField::create('Sections', 'Sections', $this->Sections()->sort('SortOrder'), $config); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             $fields->addFieldsToTab('Root.Sections', array( | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                 $sectionsField, | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 1 |  |             )); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         return $fields; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function getPageSections() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return $this->Sections()->sort('SortOrder'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  | } | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.