Conditions | 7 |
Paths | 36 |
Total Lines | 61 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
121 | public function getInputHtml($value, ElementInterface $element = null): string |
||
122 | { |
||
123 | // Register our asset bundle |
||
124 | try { |
||
125 | Craft::$app->getView()->registerAssetBundle(RecipeFieldAsset::class); |
||
126 | } catch (InvalidConfigException $e) { |
||
127 | Craft::error($e->getMessage(), __METHOD__); |
||
128 | } |
||
129 | |||
130 | // Get our id and namespace |
||
131 | $id = Craft::$app->getView()->formatInputId($this->handle); |
||
132 | $nameSpacedId = Craft::$app->getView()->namespaceInputId($id); |
||
133 | |||
134 | // Variables to pass down to our field JavaScript to let it namespace properly |
||
135 | $jsonVars = [ |
||
136 | 'id' => $id, |
||
137 | 'name' => $this->handle, |
||
138 | 'namespace' => $nameSpacedId, |
||
139 | 'prefix' => Craft::$app->getView()->namespaceInputId(''), |
||
140 | ]; |
||
141 | $jsonVars = Json::encode($jsonVars); |
||
142 | Craft::$app->getView()->registerJs("$('#{$nameSpacedId}-field').RecipeRecipe(".$jsonVars.");"); |
||
143 | |||
144 | // Set asset elements |
||
145 | $elements = []; |
||
146 | if ($value->imageId) { |
||
147 | if (is_array($value->imageId)) { |
||
148 | $value->imageId = $value->imageId[0]; |
||
149 | } |
||
150 | $elements = [Craft::$app->getAssets()->getAssetById($value->imageId)]; |
||
151 | } |
||
152 | $videoElements = []; |
||
153 | if ($value->videoId) { |
||
154 | if (is_array($value->videoId)) { |
||
155 | $value->videoId = $value->videoId[0]; |
||
156 | } |
||
157 | $videoElements = [Craft::$app->getAssets()->getAssetById($value->videoId)]; |
||
158 | } |
||
159 | |||
160 | // Render the input template |
||
161 | try { |
||
162 | return Craft::$app->getView()->renderTemplate( |
||
163 | 'recipe/_components/fields/Recipe_input', |
||
164 | [ |
||
165 | 'name' => $this->handle, |
||
166 | 'value' => $value, |
||
167 | 'field' => $this, |
||
168 | 'id' => $id, |
||
169 | 'nameSpacedId' => $nameSpacedId, |
||
170 | 'prefix' => Craft::$app->getView()->namespaceInputId(''), |
||
171 | 'assetsSourceExists' => count(Craft::$app->getAssets()->findFolders()), |
||
172 | 'elements' => $elements, |
||
173 | 'videoElements' => $videoElements, |
||
174 | 'elementType' => Asset::class, |
||
175 | 'assetSources' => $this->assetSources, |
||
176 | 'hasApiCredentials' => RecipePlugin::$plugin->settings->hasApiCredentials(), |
||
177 | ] |
||
178 | ); |
||
179 | } catch (\Throwable $e) { |
||
180 | Craft::error($e->getMessage(), __METHOD__); |
||
181 | return ''; |
||
182 | } |
||
204 |