Conditions | 11 |
Paths | 18 |
Total Lines | 84 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 9 | ||
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 |
||
124 | protected function doIt() |
||
125 | { |
||
126 | |||
127 | if (!Craft::$app->getUser()->getIsAdmin()) { |
||
128 | // Do nothing if the user is not an admin |
||
129 | return; |
||
130 | } |
||
131 | |||
132 | /** @var User $user */ |
||
133 | $user = Craft::$app->getUser()->getIdentity(); |
||
134 | if (!$user->getPreference('showFieldHandles')) { |
||
135 | // Do nothing if the user is not an admin, or if field handles aren't visible |
||
136 | return; |
||
137 | } |
||
138 | |||
139 | // Render edit source links |
||
140 | if (\version_compare(Craft::$app->getVersion(), '4.0', '<')) { |
||
141 | |||
142 | // Legacy template hooks for entries, assets and categories on Craft 3.x |
||
143 | Craft::$app->getView()->hook('cp.entries.edit.meta', function (array $context) { |
||
144 | return $this->renderEditSourceLink($context); |
||
145 | }); |
||
146 | Craft::$app->getView()->hook('cp.assets.edit.meta', function (array $context) { |
||
147 | return $this->renderEditSourceLink($context); |
||
148 | }); |
||
149 | Craft::$app->getView()->hook('cp.categories.edit.details', function (array $context) { |
||
150 | return $this->renderEditSourceLink($context); |
||
151 | }); |
||
152 | |||
153 | } else { |
||
154 | |||
155 | // Use the EVENT_DEFINE_META_FIELDS_HTML event to inject source buttons for Craft 4 |
||
156 | Event::on( |
||
157 | Element::class, |
||
158 | Element::EVENT_DEFINE_META_FIELDS_HTML, |
||
159 | function (DefineHtmlEvent $event) { |
||
160 | if ($event->static) { |
||
161 | return; |
||
162 | } |
||
163 | $event->html .= $this->renderEditSourceLink(['element' => $event->sender]); |
||
164 | } |
||
165 | ); |
||
166 | } |
||
167 | |||
168 | // Hooks that work on Craft 3.x and 4.x |
||
169 | Craft::$app->getView()->hook('cp.globals.edit.content', function (array $context) { |
||
170 | return $this->renderEditSourceLink($context); |
||
171 | }); |
||
172 | Craft::$app->getView()->hook('cp.users.edit.details', function (array $context) { |
||
173 | return $this->renderEditSourceLink($context); |
||
174 | }); |
||
175 | Craft::$app->getView()->hook('cp.commerce.product.edit.details', function (array $context) { |
||
176 | return $this->renderEditSourceLink($context); |
||
177 | }); |
||
178 | |||
179 | $request = Craft::$app->getRequest(); |
||
180 | $isAjax = $request->getIsAjax() || $request->getAcceptsJson(); |
||
181 | |||
182 | if ($isAjax) { |
||
183 | |||
184 | $segments = $request->getActionSegments(); |
||
185 | if (empty($segments) || !\is_array($segments) || $segments[count($segments) - 1] !== 'get-editor-html') { |
||
186 | return; |
||
187 | } |
||
188 | |||
189 | Craft::$app->getView()->registerJs('Craft.CpFieldInspectPlugin.initElementEditor();'); |
||
190 | |||
191 | } else { |
||
192 | |||
193 | $redirectUrl = \implode('?', \array_filter([\implode('/', $request->getSegments()), $request->getQueryStringWithoutPath()])); |
||
194 | |||
195 | $data = [ |
||
196 | 'editFieldBtnLabel' => Craft::t('cp-field-inspect', 'Edit field settings'), |
||
197 | 'baseEditFieldUrl' => \rtrim(UrlHelper::cpUrl('settings/fields/edit'), '/'), |
||
198 | 'redirectUrl' => Craft::$app->getSecurity()->hashData($redirectUrl, Craft::$app->getConfig()->getGeneral()->securityKey), |
||
199 | ]; |
||
200 | |||
201 | $fields = Craft::$app->getFields()->getAllFields('global'); |
||
202 | foreach ($fields as $field) { |
||
203 | $data['fields'][$field->handle] = (int)$field->id; |
||
204 | } |
||
205 | |||
206 | Craft::$app->getView()->registerAssetBundle(CpFieldInspectBundle::class); |
||
207 | Craft::$app->getView()->registerJs('Craft.CpFieldInspectPlugin.init(' . \json_encode($data) . ');'); |
||
208 | } |
||
244 |