| Conditions | 5 |
| Paths | 8 |
| Total Lines | 97 |
| Code Lines | 70 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 40 | public function getCMSFields() |
||
| 41 | { |
||
| 42 | $fields = parent::getCMSFields(); |
||
| 43 | |||
| 44 | $subsites = DataObject::get(Subsite::class); |
||
| 45 | if (!$subsites) { |
||
| 46 | $subsites = new ArrayList(); |
||
| 47 | } else { |
||
| 48 | $subsites = ArrayList::create($subsites->toArray()); |
||
| 49 | } |
||
| 50 | |||
| 51 | $subsites->push(new ArrayData(['Title' => 'Main site', 'ID' => 0])); |
||
| 52 | |||
| 53 | $fields->addFieldToTab( |
||
| 54 | 'Root.Main', |
||
| 55 | DropdownField::create( |
||
| 56 | 'CopyContentFromID_SubsiteID', |
||
| 57 | _t(__CLASS__ . '.SubsiteField', 'Subsite'), |
||
| 58 | $subsites->map('ID', 'Title') |
||
| 59 | )->addExtraClass('subsitestreedropdownfield-chooser no-change-track'), |
||
| 60 | 'CopyContentFromID' |
||
| 61 | ); |
||
| 62 | |||
| 63 | // Setup the linking to the original page. |
||
| 64 | $pageSelectionField = SubsitesTreeDropdownField::create( |
||
| 65 | 'CopyContentFromID', |
||
| 66 | _t('SilverStripe\\CMS\\Model\\VirtualPage.CHOOSE', 'Linked Page'), |
||
| 67 | SiteTree::class, |
||
| 68 | 'ID', |
||
| 69 | 'MenuTitle' |
||
| 70 | ); |
||
| 71 | |||
| 72 | $fields->addFieldToTab( |
||
| 73 | 'Root.Main', |
||
| 74 | TreeDropdownField::create('CopyContentFromID', 'Linked Page', SiteTree::class) |
||
| 75 | ); |
||
| 76 | |||
| 77 | if (Controller::has_curr() && Controller::curr()->getRequest()) { |
||
| 78 | $subsiteID = (int) Controller::curr()->getRequest()->requestVar('CopyContentFromID_SubsiteID'); |
||
| 79 | $pageSelectionField->setSubsiteID($subsiteID); |
||
| 80 | } |
||
| 81 | $fields->replaceField('CopyContentFromID', $pageSelectionField); |
||
| 82 | |||
| 83 | // Create links back to the original object in the CMS |
||
| 84 | if ($this->CopyContentFromID) { |
||
| 85 | $editLink = Controller::join_links( |
||
| 86 | CMSPageEditController::singleton()->Link('show'), |
||
| 87 | $this->CopyContentFromID |
||
| 88 | ); |
||
| 89 | |||
| 90 | $linkToContent = " |
||
| 91 | <a class=\"cmsEditlink\" href=\"$editLink\">" . |
||
| 92 | _t('SilverStripe\\CMS\\Model\\VirtualPage.EDITCONTENT', 'Click here to edit the content') . |
||
| 93 | '</a>'; |
||
| 94 | $fields->removeByName('VirtualPageContentLinkLabel'); |
||
| 95 | $fields->addFieldToTab( |
||
| 96 | 'Root.Main', |
||
| 97 | $linkToContentLabelField = LiteralField::create('VirtualPageContentLinkLabel', $linkToContent), |
||
| 98 | 'Title' |
||
| 99 | ); |
||
| 100 | } |
||
| 101 | |||
| 102 | |||
| 103 | $fields->addFieldToTab( |
||
| 104 | 'Root.Main', |
||
| 105 | TextField::create( |
||
| 106 | 'CustomMetaTitle', |
||
| 107 | $this->fieldLabel('CustomMetaTitle') |
||
| 108 | )->setDescription(_t(__CLASS__ . '.OverrideNote', 'Overrides inherited value from the source')), |
||
| 109 | 'MetaTitle' |
||
| 110 | ); |
||
| 111 | $fields->addFieldToTab( |
||
| 112 | 'Root.Main', |
||
| 113 | TextareaField::create( |
||
| 114 | 'CustomMetaKeywords', |
||
| 115 | $this->fieldLabel('CustomMetaKeywords') |
||
| 116 | )->setDescription(_t(__CLASS__ . '.OverrideNote', 'Overrides inherited value from the source')), |
||
| 117 | 'MetaKeywords' |
||
| 118 | ); |
||
| 119 | $fields->addFieldToTab( |
||
| 120 | 'Root.Main', |
||
| 121 | TextareaField::create( |
||
| 122 | 'CustomMetaDescription', |
||
| 123 | $this->fieldLabel('CustomMetaDescription') |
||
| 124 | )->setDescription(_t(__CLASS__ . '.OverrideNote', 'Overrides inherited value from the source')), |
||
| 125 | 'MetaDescription' |
||
| 126 | ); |
||
| 127 | $fields->addFieldToTab( |
||
| 128 | 'Root.Main', |
||
| 129 | TextField::create( |
||
| 130 | 'CustomExtraMeta', |
||
| 131 | $this->fieldLabel('CustomExtraMeta') |
||
| 132 | )->setDescription(_t(__CLASS__ . '.OverrideNote', 'Overrides inherited value from the source')), |
||
| 133 | 'ExtraMeta' |
||
| 134 | ); |
||
| 135 | |||
| 136 | return $fields; |
||
| 137 | } |
||
| 251 |