Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
6 | class ElementPageExtension extends DataExtension |
||
|
|||
7 | { |
||
8 | |||
9 | /** |
||
10 | * @config |
||
11 | * |
||
12 | * @var string $elements_title Title of the element in the CMS. |
||
13 | */ |
||
14 | private static $elements_title = 'Content Blocks'; |
||
15 | |||
16 | /** |
||
17 | * @config |
||
18 | * |
||
19 | * @var array $ignored_classes Classes to ignore adding elements too. |
||
20 | */ |
||
21 | private static $ignored_classes = array(); |
||
22 | |||
23 | /** |
||
24 | * @var array $db |
||
25 | */ |
||
26 | private static $db = array(); |
||
27 | |||
28 | /** |
||
29 | * @var array $has_one |
||
30 | */ |
||
31 | private static $has_one = array( |
||
32 | 'ElementArea' => 'ElementalArea' |
||
33 | ); |
||
34 | |||
35 | /** |
||
36 | * Setup the CMS Fields |
||
37 | * |
||
38 | * @param FieldList |
||
39 | */ |
||
40 | public function updateCMSFields(FieldList $fields) |
||
41 | { |
||
42 | if(!$this->supportsElemental()) { |
||
43 | return false; |
||
44 | } |
||
45 | |||
46 | // add an empty holder for content as some module explicitly use insert |
||
47 | // after content. |
||
48 | $fields->replaceField('Content', new LiteralField('Content', '')); |
||
49 | |||
50 | $adder = new ElementalGridFieldAddNewMultiClass(); |
||
51 | |||
52 | $list = $this->getAvailableTypes(); |
||
53 | $adder->setClasses($list); |
||
54 | |||
55 | $area = $this->owner->ElementArea(); |
||
56 | |||
57 | if (!$area->exists() || !$area->isInDB()) { |
||
58 | $area->write(); |
||
59 | |||
60 | $this->owner->ElementAreaID = $area->ID; |
||
61 | $this->owner->write(); |
||
62 | } |
||
63 | |||
64 | $gridField = GridField::create('ElementArea', |
||
65 | Config::inst()->get("ElementPageExtension", 'elements_title'), |
||
66 | $this->owner->ElementArea()->Elements(), |
||
67 | GridFieldConfig_RelationEditor::create() |
||
68 | ->removeComponentsByType('GridFieldAddNewButton') |
||
69 | ->removeComponentsByType('GridFieldDeleteAction') |
||
70 | ->removeComponentsByType('GridFieldAddExistingAutocompleter') |
||
71 | ->addComponent(new ElementalGridFieldAddExistingAutocompleter()) |
||
72 | ->addComponent(new ElementalGridFieldDeleteAction()) |
||
73 | ->addComponent($adder) |
||
74 | ->addComponent(new GridFieldSortableRows('Sort')) |
||
75 | ); |
||
76 | |||
77 | $config = $gridField->getConfig(); |
||
78 | $paginator = $config->getComponentByType('GridFieldPaginator'); |
||
79 | $paginator->setItemsPerPage(100); |
||
80 | |||
81 | $config->removeComponentsByType('GridFieldDetailForm'); |
||
82 | $config->addComponent(new VersionedDataObjectDetailsForm()); |
||
83 | |||
84 | $fields->addFieldToTab('Root.Main', $gridField, 'Metadata'); |
||
85 | |||
86 | return $fields; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @return array |
||
91 | */ |
||
92 | public function getAvailableTypes() { |
||
116 | |||
117 | /** |
||
118 | * Make sure there is always a WidgetArea sidebar for adding widgets |
||
119 | * |
||
120 | */ |
||
121 | public function onBeforeWrite() |
||
165 | |||
166 | /** |
||
167 | * @return boolean |
||
168 | */ |
||
169 | public function supportsElemental() { |
||
182 | |||
183 | /** |
||
184 | * If the page is duplicated, copy the widgets across too. |
||
185 | * |
||
186 | * Gets called twice from either direction, due to bad DataObject and SiteTree code, hence the weird if statement |
||
187 | * |
||
188 | * @return Page The duplicated page |
||
189 | */ |
||
190 | public function onAfterDuplicate($duplicatePage) |
||
207 | |||
208 | /** |
||
209 | * If the page is duplicated across subsites, copy the widgets across too. |
||
210 | * |
||
211 | * @return Page The duplicated page |
||
212 | */ |
||
213 | public function onAfterDuplicateToSubsite($originalPage) |
||
228 | |||
229 | /** |
||
230 | * Publish |
||
231 | */ |
||
232 | public function onAfterPublish() |
||
254 | |||
255 | /** |
||
256 | * Roll back all changes if the parent page has a rollback event |
||
257 | * |
||
258 | * Only do rollback if it's the 'cancel draft changes' rollback, not a specific version |
||
259 | * rollback. |
||
260 | * |
||
261 | * @param string $version |
||
262 | * @return null |
||
263 | */ |
||
264 | public function onBeforeRollback($version) |
||
285 | } |
||
286 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.