Complex classes like EditableFormField often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EditableFormField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class EditableFormField extends DataObject { |
||
|
|
|||
| 16 | |||
| 17 | /** |
||
| 18 | * Set to true to hide from class selector |
||
| 19 | * |
||
| 20 | * @config |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | private static $hidden = false; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Define this field as abstract (not inherited) |
||
| 27 | * |
||
| 28 | * @config |
||
| 29 | * @var bool |
||
| 30 | */ |
||
| 31 | private static $abstract = true; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Flag this field type as non-data (e.g. literal, header, html) |
||
| 35 | * |
||
| 36 | * @config |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | private static $literal = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Default sort order |
||
| 43 | * |
||
| 44 | * @config |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private static $default_sort = '"Sort"'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * A list of CSS classes that can be added |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | public static $allowed_css = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @config |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | private static $summary_fields = array( |
||
| 61 | 'Title' |
||
| 62 | ); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @config |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private static $db = array( |
||
| 69 | "Name" => "Varchar", |
||
| 70 | "Title" => "Varchar(255)", |
||
| 71 | "Default" => "Varchar(255)", |
||
| 72 | "Sort" => "Int", |
||
| 73 | "Required" => "Boolean", |
||
| 74 | "CustomErrorMessage" => "Varchar(255)", |
||
| 75 | |||
| 76 | "CustomRules" => "Text", // @deprecated from 2.0 |
||
| 77 | "CustomSettings" => "Text", // @deprecated from 2.0 |
||
| 78 | "Migrated" => "Boolean", // set to true when migrated |
||
| 79 | |||
| 80 | "ExtraClass" => "Text", // from CustomSettings |
||
| 81 | "RightTitle" => "Varchar(255)", // from CustomSettings |
||
| 82 | "ShowOnLoad" => "Boolean(1)", // from CustomSettings |
||
| 83 | ); |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @config |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | private static $has_one = array( |
||
| 90 | "Parent" => "UserDefinedForm", |
||
| 91 | ); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Built in extensions required |
||
| 95 | * |
||
| 96 | * @config |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | private static $extensions = array( |
||
| 100 | "Versioned('Stage', 'Live')" |
||
| 101 | ); |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @config |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | private static $has_many = array( |
||
| 108 | "DisplayRules" => "EditableCustomRule.Parent" // from CustomRules |
||
| 109 | ); |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var bool |
||
| 113 | */ |
||
| 114 | protected $readonly; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Set the visibility of an individual form field |
||
| 118 | * |
||
| 119 | * @param bool |
||
| 120 | */ |
||
| 121 | public function setReadonly($readonly = true) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Returns whether this field is readonly |
||
| 127 | * |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | private function isReadonly() { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return FieldList |
||
| 136 | */ |
||
| 137 | public function getCMSFields() { |
||
| 138 | $fields = new FieldList(new TabSet('Root')); |
||
| 139 | |||
| 140 | // Main tab |
||
| 141 | $fields->addFieldsToTab( |
||
| 142 | 'Root.Main', |
||
| 143 | array( |
||
| 144 | ReadonlyField::create( |
||
| 145 | 'Type', |
||
| 146 | _t('EditableFormField.TYPE', 'Type'), |
||
| 147 | $this->i18n_singular_name() |
||
| 148 | ), |
||
| 149 | LiteralField::create( |
||
| 150 | 'MergeField', |
||
| 151 | _t( |
||
| 152 | 'EditableFormField.MERGEFIELDNAME', |
||
| 153 | '<div class="field readonly">' . |
||
| 154 | '<label class="left">Merge field</label>' . |
||
| 155 | '<div class="middleColumn">' . |
||
| 156 | '<span class="readonly">$' . $this->Name . '</span>' . |
||
| 157 | '</div>' . |
||
| 158 | '</div>' |
||
| 159 | ) |
||
| 160 | ), |
||
| 161 | TextField::create('Title'), |
||
| 162 | TextField::create('Default', _t('EditableFormField.DEFAULT', 'Default value')), |
||
| 163 | TextField::create('RightTitle', _t('EditableFormField.RIGHTTITLE', 'Right title')), |
||
| 164 | SegmentField::create('Name')->setModifiers(array( |
||
| 165 | UnderscoreSegmentFieldModifier::create()->setDefault('FieldName'), |
||
| 166 | DisambiguationSegmentFieldModifier::create(), |
||
| 167 | ))->setPreview($this->Name) |
||
| 168 | ) |
||
| 169 | ); |
||
| 170 | |||
| 171 | // Custom settings |
||
| 172 | if (!empty(self::$allowed_css)) { |
||
| 173 | $cssList = array(); |
||
| 174 | foreach(self::$allowed_css as $k => $v) { |
||
| 175 | if (!is_array($v)) { |
||
| 176 | $cssList[$k]=$v; |
||
| 177 | } elseif ($k === $this->ClassName) { |
||
| 178 | $cssList = array_merge($cssList, $v); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | $fields->addFieldToTab('Root.Main', |
||
| 183 | DropdownField::create( |
||
| 184 | 'ExtraClass', |
||
| 185 | _t('EditableFormField.EXTRACLASS_TITLE', 'Extra Styling/Layout'), |
||
| 186 | $cssList |
||
| 187 | )->setDescription(_t( |
||
| 188 | 'EditableFormField.EXTRACLASS_SELECT', |
||
| 189 | 'Select from the list of allowed styles' |
||
| 190 | )) |
||
| 191 | ); |
||
| 192 | } else { |
||
| 193 | $fields->addFieldToTab('Root.Main', |
||
| 194 | TextField::create( |
||
| 195 | 'ExtraClass', |
||
| 196 | _t('EditableFormField.EXTRACLASS_Title', 'Extra CSS classes') |
||
| 197 | )->setDescription(_t( |
||
| 198 | 'EditableFormField.EXTRACLASS_MULTIPLE', |
||
| 199 | 'Separate each CSS class with a single space' |
||
| 200 | )) |
||
| 201 | ); |
||
| 202 | } |
||
| 203 | |||
| 204 | // Validation |
||
| 205 | $validationFields = $this->getFieldValidationOptions(); |
||
| 206 | if($validationFields) { |
||
| 207 | $fields->addFieldsToTab( |
||
| 208 | 'Root.Validation', |
||
| 209 | $this->getFieldValidationOptions() |
||
| 210 | ); |
||
| 211 | } |
||
| 212 | $allowedClasses = array_keys($this->getEditableFieldClasses(false)); |
||
| 213 | $self = $this; |
||
| 214 | $editableColumns = new GridFieldEditableColumns(); |
||
| 215 | $editableColumns->setDisplayFields(array( |
||
| 216 | 'Display' => '', |
||
| 217 | 'ConditionFieldID' => function($record, $column, $grid) use ($allowedClasses, $self) { |
||
| 218 | return DropdownField::create( |
||
| 219 | $column, |
||
| 220 | '', |
||
| 221 | EditableFormField::get() |
||
| 222 | ->filter(array( |
||
| 223 | 'ParentID' => $self->ParentID, |
||
| 224 | 'ClassName' => $allowedClasses |
||
| 225 | )) |
||
| 226 | ->exclude(array( |
||
| 227 | 'ID' => $self->ID |
||
| 228 | )) |
||
| 229 | ->map('ID', 'Title') |
||
| 230 | ); |
||
| 231 | }, |
||
| 232 | 'ConditionOption' => function($record, $column, $grid) { |
||
| 233 | $options = Config::inst()->get('EditableCustomRule', 'condition_options'); |
||
| 234 | return DropdownField::create($column, '', $options); |
||
| 235 | }, |
||
| 236 | 'FieldValue' => function($record, $column, $grid) { |
||
| 237 | return TextField::create($column); |
||
| 238 | }, |
||
| 239 | 'ParentID' => function($record, $column, $grid) use ($self) { |
||
| 240 | return HiddenField::create($column, '', $self->ID); |
||
| 241 | } |
||
| 242 | )); |
||
| 243 | |||
| 244 | // Custom rules |
||
| 245 | $customRulesConfig = GridFieldConfig::create() |
||
| 246 | ->addComponents( |
||
| 247 | $editableColumns, |
||
| 248 | new GridFieldButtonRow(), |
||
| 249 | new GridFieldToolbarHeader(), |
||
| 250 | new GridFieldAddNewInlineButton(), |
||
| 251 | new GridFieldDeleteAction() |
||
| 252 | ); |
||
| 253 | |||
| 254 | $fields->addFieldsToTab('Root.DisplayRules', array( |
||
| 255 | CheckboxField::create('ShowOnLoad') |
||
| 256 | ->setDescription(_t( |
||
| 257 | 'EditableFormField.SHOWONLOAD', |
||
| 258 | 'Initial visibility before processing these rules' |
||
| 259 | )), |
||
| 260 | GridField::create( |
||
| 261 | 'DisplayRules', |
||
| 262 | _t('EditableFormField.CUSTOMRULES', 'Custom Rules'), |
||
| 263 | $this->DisplayRules(), |
||
| 264 | $customRulesConfig |
||
| 265 | ) |
||
| 266 | )); |
||
| 267 | |||
| 268 | $this->extend('updateCMSFields', $fields); |
||
| 269 | |||
| 270 | return $fields; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @return void |
||
| 275 | */ |
||
| 276 | public function onBeforeWrite() { |
||
| 277 | parent::onBeforeWrite(); |
||
| 278 | |||
| 279 | // Set a field name. |
||
| 280 | if(!$this->Name) { |
||
| 281 | // New random name |
||
| 282 | $this->Name = $this->generateName(); |
||
| 283 | |||
| 284 | } elseif($this->Name === 'Field') { |
||
| 285 | throw new ValidationException('Field name cannot be "Field"'); |
||
| 286 | } |
||
| 287 | |||
| 288 | if(!$this->Sort && $this->ParentID) { |
||
| 289 | $parentID = $this->ParentID; |
||
| 290 | $this->Sort = EditableFormField::get() |
||
| 291 | ->filter('ParentID', $parentID) |
||
| 292 | ->max('Sort') + 1; |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Generate a new non-conflicting Name value |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | protected function generateName() { |
||
| 302 | do { |
||
| 303 | // Generate a new random name after this class |
||
| 304 | $class = get_class($this); |
||
| 305 | $entropy = substr(sha1(uniqid()), 0, 5); |
||
| 306 | $name = "{$class}_{$entropy}"; |
||
| 307 | |||
| 308 | // Check if it conflicts |
||
| 309 | $exists = EditableFormField::get()->filter('Name', $name)->count() > 0; |
||
| 310 | } while($exists); |
||
| 311 | return $name; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Flag indicating that this field will set its own error message via data-msg='' attributes |
||
| 316 | * |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | public function getSetsOwnError() { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Return whether a user can delete this form field |
||
| 325 | * based on whether they can edit the page |
||
| 326 | * |
||
| 327 | * @return bool |
||
| 328 | */ |
||
| 329 | public function canDelete($member = null) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Return whether a user can edit this form field |
||
| 335 | * based on whether they can edit the page |
||
| 336 | * |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 339 | public function canEdit($member = null) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Return whether a user can view this form field |
||
| 349 | * based on whether they can view the page, regardless of the ReadOnly status of the field |
||
| 350 | * |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | public function canView($member = null) { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Return whether a user can create this form field |
||
| 363 | * based on whether they can edit the page |
||
| 364 | * |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | public function canCreate($member = null, $context = array()) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Publish this Form Field to the live site |
||
| 389 | * |
||
| 390 | * Wrapper for the {@link Versioned} publish function |
||
| 391 | */ |
||
| 392 | public function doPublish($fromStage, $toStage, $createNewVersion = false) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Delete this field from a given stage |
||
| 403 | * |
||
| 404 | * Wrapper for the {@link Versioned} deleteFromStage function |
||
| 405 | */ |
||
| 406 | public function doDeleteFromStage($stage) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * checks wether record is new, copied from Sitetree |
||
| 420 | */ |
||
| 421 | function isNew() { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * checks if records is changed on stage |
||
| 431 | * @return boolean |
||
| 432 | */ |
||
| 433 | public function getIsModifiedOnStage() { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @deprecated since version 4.0 |
||
| 445 | */ |
||
| 446 | public function getSettings() { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @deprecated since version 4.0 |
||
| 453 | */ |
||
| 454 | public function setSettings($settings = array()) { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @deprecated since version 4.0 |
||
| 461 | */ |
||
| 462 | public function setSetting($key, $value) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Set the allowed css classes for the extraClass custom setting |
||
| 472 | * |
||
| 473 | * @param array The permissible CSS classes to add |
||
| 474 | */ |
||
| 475 | public function setAllowedCss(array $allowed) { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @deprecated since version 4.0 |
||
| 485 | */ |
||
| 486 | public function getSetting($setting) { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Get the path to the icon for this field type, relative to the site root. |
||
| 500 | * |
||
| 501 | * @return string |
||
| 502 | */ |
||
| 503 | public function getIcon() { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Return whether or not this field has addable options |
||
| 509 | * such as a dropdown field or radio set |
||
| 510 | * |
||
| 511 | * @return bool |
||
| 512 | */ |
||
| 513 | public function getHasAddableOptions() { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Return whether or not this field needs to show the extra |
||
| 519 | * options dropdown list |
||
| 520 | * |
||
| 521 | * @return bool |
||
| 522 | */ |
||
| 523 | public function showExtraOptions() { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Returns the Title for rendering in the front-end (with XML values escaped) |
||
| 529 | * |
||
| 530 | * @return string |
||
| 531 | */ |
||
| 532 | public function getEscapedTitle() { |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Find the numeric indicator (1.1.2) that represents it's nesting value |
||
| 538 | * |
||
| 539 | * Only useful for fields attached to a current page, and that contain other fields such as pages |
||
| 540 | * or groups |
||
| 541 | * |
||
| 542 | * @return string |
||
| 543 | */ |
||
| 544 | public function getFieldNumber() { |
||
| 574 | |||
| 575 | public function getCMSTitle() { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @deprecated since version 4.0 |
||
| 581 | */ |
||
| 582 | public function getFieldName($field = false) { |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @deprecated since version 4.0 |
||
| 589 | */ |
||
| 590 | public function getSettingName($field) { |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Append custom validation fields to the default 'Validation' |
||
| 599 | * section in the editable options view |
||
| 600 | * |
||
| 601 | * @return FieldList |
||
| 602 | */ |
||
| 603 | public function getFieldValidationOptions() { |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Return a FormField to appear on the front end. Implement on |
||
| 617 | * your subclass. |
||
| 618 | * |
||
| 619 | * @return FormField |
||
| 620 | */ |
||
| 621 | public function getFormField() { |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Updates a formfield with extensions |
||
| 627 | * |
||
| 628 | * @param FormField $field |
||
| 629 | */ |
||
| 630 | public function doUpdateFormField($field) { |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Updates a formfield with the additional metadata specified by this field |
||
| 638 | * |
||
| 639 | * @param FormField $field |
||
| 640 | */ |
||
| 641 | protected function updateFormField($field) { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Return the instance of the submission field class |
||
| 673 | * |
||
| 674 | * @return SubmittedFormField |
||
| 675 | */ |
||
| 676 | public function getSubmittedFormField() { |
||
| 679 | |||
| 680 | |||
| 681 | /** |
||
| 682 | * Show this form field (and its related value) in the reports and in emails. |
||
| 683 | * |
||
| 684 | * @return bool |
||
| 685 | */ |
||
| 686 | public function showInReports() { |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Return the error message for this field. Either uses the custom |
||
| 692 | * one (if provided) or the default SilverStripe message |
||
| 693 | * |
||
| 694 | * @return Varchar |
||
| 695 | */ |
||
| 696 | public function getErrorMessage() { |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Validate the field taking into account its custom rules. |
||
| 708 | * |
||
| 709 | * @param Array $data |
||
| 710 | * @param UserForm $form |
||
| 711 | * |
||
| 712 | * @return boolean |
||
| 713 | */ |
||
| 714 | public function validateField($data, $form) { |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Invoked by UserFormUpgradeService to migrate settings specific to this field from CustomSettings |
||
| 736 | * to the field proper |
||
| 737 | * |
||
| 738 | * @param array $data Unserialised data |
||
| 739 | */ |
||
| 740 | public function migrateSettings($data) { |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Get the formfield to use when editing this inline in gridfield |
||
| 757 | * |
||
| 758 | * @param string $column name of column |
||
| 759 | * @param array $fieldClasses List of allowed classnames if this formfield has a selectable class |
||
| 760 | * @return FormField |
||
| 761 | */ |
||
| 762 | public function getInlineClassnameField($column, $fieldClasses) { |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Get the formfield to use when editing the title inline |
||
| 768 | * |
||
| 769 | * @param string $column |
||
| 770 | * @return FormField |
||
| 771 | */ |
||
| 772 | public function getInlineTitleField($column) { |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Get the JS expression for selecting the holder for this field |
||
| 780 | * |
||
| 781 | * @return string |
||
| 782 | */ |
||
| 783 | public function getSelectorHolder() { |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Gets the JS expression for selecting the value for this field |
||
| 789 | * |
||
| 790 | * @param EditableCustomRule $rule Custom rule this selector will be used with |
||
| 791 | * @param bool $forOnLoad Set to true if this will be invoked on load |
||
| 792 | */ |
||
| 793 | public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false) { |
||
| 796 | |||
| 797 | |||
| 798 | /** |
||
| 799 | * Get the list of classes that can be selected and used as data-values |
||
| 800 | * |
||
| 801 | * @param $includeLiterals Set to false to exclude non-data fields |
||
| 802 | * @return array |
||
| 803 | */ |
||
| 804 | public function getEditableFieldClasses($includeLiterals = true) { |
||
| 831 | |||
| 832 | /** |
||
| 833 | * @return EditableFormFieldValidator |
||
| 834 | */ |
||
| 835 | public function getCMSValidator() { |
||
| 839 | } |
||
| 840 |
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.