11
11
use Craft ;
12
12
use craft \base \Element ;
13
13
use craft \base \ElementInterface ;
14
+ use craft \base \Event ;
14
15
use craft \base \Field ;
15
16
use craft \base \PreviewableFieldInterface ;
17
+ use craft \elements \db \ElementQuery ;
16
18
use craft \elements \db \ElementQueryInterface ;
17
19
use craft \errors \InvalidFieldException ;
20
+ use craft \events \CancelableEvent ;
18
21
use craft \helpers \Json ;
19
22
use ether \simplemap \enums \GeoService as GeoEnum ;
20
23
use ether \simplemap \enums \MapTiles ;
32
35
use Twig \Error \SyntaxError ;
33
36
use Twig \Markup ;
34
37
use yii \base \InvalidConfigException ;
38
+ use yii \db \ExpressionInterface ;
35
39
use yii \db \Schema ;
36
40
37
41
/**
@@ -155,9 +159,22 @@ class MapField extends Field implements PreviewableFieldInterface
155
159
*/
156
160
public string $ boundary = '"" ' ;
157
161
162
+ private static $ searchParams = null ;
163
+
158
164
// Methods
159
165
// =========================================================================
160
166
167
+ public function init (): void
168
+ {
169
+ Event::on (
170
+ ElementQuery::class,
171
+ ElementQuery::EVENT_AFTER_PREPARE ,
172
+ [$ this , 'afterPrepareElementQuery ' ],
173
+ );
174
+
175
+ parent ::init ();
176
+ }
177
+
161
178
// Methods: Static
162
179
// -------------------------------------------------------------------------
163
180
@@ -192,6 +209,19 @@ public static function supportedTranslationMethods (): array
192
209
];
193
210
}
194
211
212
+ public static function queryCondition (array $ instances , mixed $ value , array &$ params ): array |string |ExpressionInterface |false |null
213
+ {
214
+ if (empty ($ instances ) || empty ($ value ))
215
+ return null ;
216
+
217
+ self ::$ searchParams = [
218
+ 'field ' => $ instances [0 ],
219
+ 'value ' => $ value ,
220
+ ];
221
+
222
+ return null ;
223
+ }
224
+
195
225
// Methods: Instance
196
226
// -------------------------------------------------------------------------
197
227
@@ -233,13 +263,6 @@ public function rules (): array
233
263
return $ rules ;
234
264
}
235
265
236
- /**
237
- * @param mixed $value
238
- * @param ElementInterface|Element|null $element
239
- *
240
- * @return Map
241
- * @throws Exception
242
- */
243
266
public function normalizeValue (mixed $ value , ElementInterface |Element $ element = null ): Map
244
267
{
245
268
if (is_string ($ value ))
@@ -272,14 +295,6 @@ public function normalizeValue (mixed $value, ElementInterface|Element $element
272
295
return $ map ;
273
296
}
274
297
275
- /**
276
- * @return string
277
- * @throws InvalidConfigException
278
- * @throws LoaderError
279
- * @throws RuntimeError
280
- * @throws SyntaxError
281
- * @throws \yii\base\Exception
282
- */
283
298
public function getSettingsHtml (): string
284
299
{
285
300
$ value = new Map ();
@@ -337,13 +352,6 @@ public function getSettingsHtml (): string
337
352
]);
338
353
}
339
354
340
- /**
341
- * @param null $value
342
- * @param ElementInterface|null $element
343
- *
344
- * @return string
345
- * @throws InvalidConfigException
346
- */
347
355
public function getInputHtml ($ value = null , ElementInterface $ element = null ): string
348
356
{
349
357
if ($ element !== null && $ element ->hasEagerLoadedElements ($ this ->handle ))
@@ -355,39 +363,11 @@ public function getInputHtml ($value = null, ElementInterface $element = null):
355
363
);
356
364
}
357
365
358
- /**
359
- * @inheritdoc
360
- *
361
- * @param mixed $value
362
- * @param ElementInterface $element
363
- *
364
- * @return string
365
- * @throws Exception
366
- */
367
366
public function getTableAttributeHtml (mixed $ value , ElementInterface $ element ): string
368
367
{
369
368
return $ this ->normalizeValue ($ value , $ element )->address ;
370
369
}
371
370
372
- /**
373
- * @param ElementQueryInterface $query
374
- * @param $value
375
- *
376
- * @return void
377
- * @throws Exception
378
- */
379
- public function modifyElementsQuery (ElementQueryInterface $ query , $ value ): void
380
- {
381
- if (!SimpleMap::getInstance ())
382
- return ;
383
-
384
- SimpleMap::getInstance ()->map ->modifyElementsQuery ($ query , $ value , $ this );
385
- }
386
-
387
- /**
388
- * @inheritdoc
389
- * @throws Exception
390
- */
391
371
public function isValueEmpty ($ value , ElementInterface $ element ): bool
392
372
{
393
373
return $ this ->normalizeValue ($ value )->isValueEmpty ();
@@ -417,9 +397,6 @@ public function getContentGqlMutationArgumentType (): Type|array
417
397
// Methods: Events
418
398
// -------------------------------------------------------------------------
419
399
420
- /**
421
- * @inheritdoc
422
- */
423
400
public function beforeSave (bool $ isNew ): bool
424
401
{
425
402
$ this ->lat = (float ) $ this ->lat ;
@@ -432,13 +409,6 @@ public function beforeSave (bool $isNew): bool
432
409
return parent ::beforeSave ($ isNew );
433
410
}
434
411
435
- /**
436
- * @param ElementInterface $element
437
- * @param bool $isNew
438
- *
439
- * @return bool
440
- * @throws InvalidFieldException
441
- */
442
412
public function beforeElementSave (ElementInterface $ element , bool $ isNew ): bool
443
413
{
444
414
if (!SimpleMap::getInstance ()->map ->validateField ($ this , $ element ))
@@ -447,20 +417,27 @@ public function beforeElementSave (ElementInterface $element, bool $isNew): bool
447
417
return parent ::beforeElementSave ($ element , $ isNew );
448
418
}
449
419
450
- /**
451
- * @param ElementInterface $element
452
- * @param bool $isNew
453
- *
454
- * @throws InvalidFieldException
455
- * @throws Throwable
456
- */
457
420
public function afterElementSave (ElementInterface $ element , bool $ isNew ): void
458
421
{
459
422
SimpleMap::getInstance ()->map ->saveField ($ this , $ element );
460
423
461
424
parent ::afterElementSave ($ element , $ isNew );
462
425
}
463
426
427
+ public function afterPrepareElementQuery (CancelableEvent $ event ): void
428
+ {
429
+ if (!self ::$ searchParams ) return ;
430
+
431
+ /** @var ElementQueryInterface $query */
432
+ $ query = $ event ->sender ;
433
+
434
+ SimpleMap::getInstance ()->map ->modifyElementsQuery (
435
+ $ query ,
436
+ self ::$ searchParams ['value ' ],
437
+ self ::$ searchParams ['field ' ],
438
+ );
439
+ }
440
+
464
441
// Helpers
465
442
// =========================================================================
466
443
0 commit comments