1 | <?php |
||||||
2 | /** |
||||||
3 | * Recipe plugin for Craft CMS 3.x |
||||||
4 | * |
||||||
5 | * A comprehensive recipe FieldType for Craft CMS that includes metric/imperial |
||||||
6 | * conversion, portion calculation, and JSON-LD microdata support |
||||||
7 | * |
||||||
8 | * @link https://nystudio107.com |
||||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||||
9 | * @copyright Copyright (c) 2017 nystudio107 |
||||||
0 ignored issues
–
show
|
|||||||
10 | */ |
||||||
0 ignored issues
–
show
|
|||||||
11 | |||||||
12 | namespace nystudio107\recipe\fields; |
||||||
13 | |||||||
14 | use Craft; |
||||||
15 | use craft\base\ElementInterface; |
||||||
0 ignored issues
–
show
The type
craft\base\ElementInterface was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||||
16 | use craft\base\Field; |
||||||
17 | use craft\base\PreviewableFieldInterface; |
||||||
18 | use craft\elements\Asset; |
||||||
0 ignored issues
–
show
The type
craft\elements\Asset was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||||
19 | use craft\helpers\Html; |
||||||
20 | use craft\helpers\Json; |
||||||
21 | use nystudio107\recipe\assetbundles\recipefield\RecipeFieldAsset; |
||||||
22 | use nystudio107\recipe\models\Recipe as RecipeModel; |
||||||
23 | use nystudio107\recipe\models\Settings; |
||||||
24 | use nystudio107\recipe\Recipe as RecipePlugin; |
||||||
25 | use Throwable; |
||||||
26 | use yii\base\InvalidConfigException; |
||||||
27 | use yii\db\Schema; |
||||||
28 | |||||||
29 | /** |
||||||
0 ignored issues
–
show
|
|||||||
30 | * @author nystudio107 |
||||||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||||||
31 | * @package Recipe |
||||||
0 ignored issues
–
show
|
|||||||
32 | * @since 1.0.0 |
||||||
0 ignored issues
–
show
|
|||||||
33 | */ |
||||||
0 ignored issues
–
show
|
|||||||
34 | class Recipe extends Field implements PreviewableFieldInterface |
||||||
35 | { |
||||||
36 | // Public Properties |
||||||
37 | // ========================================================================= |
||||||
38 | |||||||
39 | /** |
||||||
0 ignored issues
–
show
|
|||||||
40 | * @var array |
||||||
41 | */ |
||||||
42 | public array $assetSources = []; |
||||||
43 | |||||||
44 | // Static Methods |
||||||
45 | // ========================================================================= |
||||||
46 | |||||||
47 | /** |
||||||
0 ignored issues
–
show
|
|||||||
48 | * @inheritdoc |
||||||
49 | */ |
||||||
0 ignored issues
–
show
|
|||||||
50 | public static function dbType(): array|string|null |
||||||
51 | { |
||||||
52 | return Schema::TYPE_TEXT; |
||||||
53 | } |
||||||
54 | |||||||
55 | /** |
||||||
0 ignored issues
–
show
|
|||||||
56 | * @inheritdoc |
||||||
57 | */ |
||||||
0 ignored issues
–
show
|
|||||||
58 | public static function displayName(): string |
||||||
59 | { |
||||||
60 | return Craft::t('recipe', 'Recipe'); |
||||||
61 | } |
||||||
62 | |||||||
63 | /** |
||||||
0 ignored issues
–
show
|
|||||||
64 | * @inheritdoc |
||||||
65 | */ |
||||||
0 ignored issues
–
show
|
|||||||
66 | public static function icon(): string |
||||||
67 | { |
||||||
68 | return 'hat-chef'; |
||||||
69 | } |
||||||
70 | |||||||
71 | /** |
||||||
0 ignored issues
–
show
|
|||||||
72 | * @inheritdoc |
||||||
73 | */ |
||||||
0 ignored issues
–
show
|
|||||||
74 | public static function phpType(): string |
||||||
75 | { |
||||||
76 | return sprintf('\\%s', RecipeModel::class); |
||||||
77 | } |
||||||
78 | |||||||
79 | // Public Methods |
||||||
80 | // ========================================================================= |
||||||
81 | |||||||
82 | /** |
||||||
0 ignored issues
–
show
|
|||||||
83 | * @inheritdoc |
||||||
84 | */ |
||||||
0 ignored issues
–
show
|
|||||||
85 | public function rules(): array |
||||||
86 | { |
||||||
87 | $rules = parent::rules(); |
||||||
88 | |||||||
89 | return array_merge($rules, [ |
||||||
0 ignored issues
–
show
|
|||||||
90 | ]); |
||||||
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||||||
91 | } |
||||||
92 | |||||||
93 | /** |
||||||
0 ignored issues
–
show
|
|||||||
94 | * @inheritdoc |
||||||
95 | * @since 1.2.1 |
||||||
0 ignored issues
–
show
|
|||||||
96 | */ |
||||||
0 ignored issues
–
show
|
|||||||
97 | public function useFieldset(): bool |
||||||
98 | { |
||||||
99 | return true; |
||||||
100 | } |
||||||
101 | |||||||
102 | /** |
||||||
0 ignored issues
–
show
|
|||||||
103 | * @inheritdoc |
||||||
104 | */ |
||||||
0 ignored issues
–
show
|
|||||||
105 | public function normalizeValue(mixed $value, ?ElementInterface $element = null): RecipeModel |
||||||
106 | { |
||||||
107 | $config = []; |
||||||
108 | // If we already have a model, just return it |
||||||
109 | if ($value instanceof RecipeModel) { |
||||||
110 | return $value; |
||||||
111 | } |
||||||
112 | // If we have a non-empty string, try to JSON-decode it |
||||||
113 | if (is_string($value)) { |
||||||
114 | $value = Json::decodeIfJson($value); |
||||||
115 | } |
||||||
116 | // If we have an array, use that for our config data |
||||||
117 | if (is_array($value)) { |
||||||
118 | $config = $value; |
||||||
119 | } |
||||||
120 | // Ensure we save our asset ids as integers, not arrays |
||||||
121 | if (isset($config['imageId'])) { |
||||||
122 | if (is_array($config['imageId'])) { |
||||||
123 | $config['imageId'] = $config['imageId'][0]; |
||||||
124 | } |
||||||
125 | } |
||||||
126 | if (isset($config['videoId'])) { |
||||||
127 | if (is_array($config['videoId'])) { |
||||||
128 | $config['videoId'] = $config['videoId'][0]; |
||||||
129 | } |
||||||
130 | } |
||||||
131 | |||||||
132 | return new RecipeModel($config); |
||||||
133 | } |
||||||
134 | |||||||
135 | /** |
||||||
0 ignored issues
–
show
|
|||||||
136 | * @inheritdoc |
||||||
137 | */ |
||||||
0 ignored issues
–
show
|
|||||||
138 | public function getSettingsHtml(): ?string |
||||||
139 | { |
||||||
140 | // Render the settings template |
||||||
141 | return Craft::$app->getView()->renderTemplate( |
||||||
142 | 'recipe/_components/fields/Recipe_settings', |
||||||
143 | [ |
||||||
144 | 'field' => $this, |
||||||
145 | 'assetSources' => $this->getSourceOptions(), |
||||||
146 | ] |
||||||
147 | ); |
||||||
148 | } |
||||||
149 | |||||||
150 | /** |
||||||
0 ignored issues
–
show
|
|||||||
151 | * @inheritdoc |
||||||
152 | */ |
||||||
0 ignored issues
–
show
|
|||||||
153 | public function getInputHtml(mixed $value, ?ElementInterface $element = null): string |
||||||
154 | { |
||||||
155 | // Register our asset bundle |
||||||
156 | try { |
||||||
157 | Craft::$app->getView()->registerAssetBundle(RecipeFieldAsset::class); |
||||||
158 | } catch (InvalidConfigException $invalidConfigException) { |
||||||
159 | Craft::error($invalidConfigException->getMessage(), __METHOD__); |
||||||
160 | } |
||||||
161 | |||||||
162 | // Get our id and namespace |
||||||
163 | $id = Html::id($this->handle); |
||||||
164 | $nameSpacedId = Craft::$app->getView()->namespaceInputId($id); |
||||||
165 | |||||||
166 | // Variables to pass down to our field JavaScript to let it namespace properly |
||||||
167 | $jsonVars = [ |
||||||
168 | 'id' => $id, |
||||||
169 | 'name' => $this->handle, |
||||||
170 | 'namespace' => $nameSpacedId, |
||||||
171 | 'prefix' => Craft::$app->getView()->namespaceInputId(''), |
||||||
172 | ]; |
||||||
173 | $jsonVars = Json::encode($jsonVars); |
||||||
174 | Craft::$app->getView()->registerJs(sprintf('$(\'#%s-field\').RecipeRecipe(', $nameSpacedId) . $jsonVars . ");"); |
||||||
175 | |||||||
176 | // Set asset elements |
||||||
177 | $elements = []; |
||||||
178 | if ($value->imageId) { |
||||||
179 | if (is_array($value->imageId)) { |
||||||
180 | $value->imageId = $value->imageId[0]; |
||||||
181 | } |
||||||
182 | |||||||
183 | $elements = [Craft::$app->getAssets()->getAssetById($value->imageId)]; |
||||||
184 | } |
||||||
185 | |||||||
186 | $videoElements = []; |
||||||
187 | if ($value->videoId) { |
||||||
188 | if (is_array($value->videoId)) { |
||||||
189 | $value->videoId = $value->videoId[0]; |
||||||
190 | } |
||||||
191 | |||||||
192 | $videoElements = [Craft::$app->getAssets()->getAssetById($value->videoId)]; |
||||||
193 | } |
||||||
194 | |||||||
195 | /** @var Settings $settings */ |
||||||
0 ignored issues
–
show
|
|||||||
196 | $settings = RecipePlugin::$plugin->getSettings(); |
||||||
0 ignored issues
–
show
The method
getSettings() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
197 | // Render the input template |
||||||
198 | try { |
||||||
199 | return Craft::$app->getView()->renderTemplate( |
||||||
200 | 'recipe/_components/fields/Recipe_input', |
||||||
201 | [ |
||||||
202 | 'name' => $this->handle, |
||||||
203 | 'value' => $value, |
||||||
204 | 'field' => $this, |
||||||
205 | 'id' => $id, |
||||||
206 | 'nameSpacedId' => $nameSpacedId, |
||||||
207 | 'prefix' => Craft::$app->getView()->namespaceInputId(''), |
||||||
208 | 'assetsSourceExists' => count(Craft::$app->getAssets()->findFolders()), |
||||||
209 | 'elements' => $elements, |
||||||
210 | 'videoElements' => $videoElements, |
||||||
211 | 'elementType' => Asset::class, |
||||||
212 | 'assetSources' => $this->assetSources, |
||||||
213 | 'hasApiCredentials' => $settings->hasApiCredentials(), |
||||||
214 | ] |
||||||
215 | ); |
||||||
216 | } catch (Throwable $throwable) { |
||||||
217 | Craft::error($throwable->getMessage(), __METHOD__); |
||||||
218 | return ''; |
||||||
219 | } |
||||||
220 | } |
||||||
221 | |||||||
222 | /** |
||||||
223 | * Get the asset sources |
||||||
224 | */ |
||||||
0 ignored issues
–
show
|
|||||||
225 | public function getSourceOptions(): array |
||||||
226 | { |
||||||
227 | $sourceOptions = []; |
||||||
228 | |||||||
229 | foreach (Asset::sources('settings') as $volume) { |
||||||
230 | if (!isset($volume['heading'])) { |
||||||
231 | $sourceOptions[] = [ |
||||||
232 | 'label' => Html::encode($volume['label']), |
||||||
233 | 'value' => $volume['key'], |
||||||
234 | ]; |
||||||
235 | } |
||||||
236 | } |
||||||
237 | |||||||
238 | return $sourceOptions; |
||||||
239 | } |
||||||
240 | } |
||||||
241 |