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