1111use Craft ;
1212use craft \base \Element ;
1313use craft \base \ElementInterface ;
14+ use craft \base \Event ;
1415use craft \base \Field ;
1516use craft \base \PreviewableFieldInterface ;
17+ use craft \elements \db \ElementQuery ;
1618use craft \elements \db \ElementQueryInterface ;
1719use craft \errors \InvalidFieldException ;
20+ use craft \events \CancelableEvent ;
1821use craft \helpers \Json ;
1922use ether \simplemap \enums \GeoService as GeoEnum ;
2023use ether \simplemap \enums \MapTiles ;
3235use Twig \Error \SyntaxError ;
3336use Twig \Markup ;
3437use yii \base \InvalidConfigException ;
38+ use yii \db \ExpressionInterface ;
3539use yii \db \Schema ;
3640
3741/**
@@ -155,9 +159,22 @@ class MapField extends Field implements PreviewableFieldInterface
155159 */
156160 public string $ boundary = '"" ' ;
157161
162+ private static $ searchParams = null ;
163+
158164 // Methods
159165 // =========================================================================
160166
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+
161178 // Methods: Static
162179 // -------------------------------------------------------------------------
163180
@@ -192,6 +209,19 @@ public static function supportedTranslationMethods (): array
192209 ];
193210 }
194211
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+
195225 // Methods: Instance
196226 // -------------------------------------------------------------------------
197227
@@ -233,13 +263,6 @@ public function rules (): array
233263 return $ rules ;
234264 }
235265
236- /**
237- * @param mixed $value
238- * @param ElementInterface|Element|null $element
239- *
240- * @return Map
241- * @throws Exception
242- */
243266 public function normalizeValue (mixed $ value , ElementInterface |Element $ element = null ): Map
244267 {
245268 if (is_string ($ value ))
@@ -272,14 +295,6 @@ public function normalizeValue (mixed $value, ElementInterface|Element $element
272295 return $ map ;
273296 }
274297
275- /**
276- * @return string
277- * @throws InvalidConfigException
278- * @throws LoaderError
279- * @throws RuntimeError
280- * @throws SyntaxError
281- * @throws \yii\base\Exception
282- */
283298 public function getSettingsHtml (): string
284299 {
285300 $ value = new Map ();
@@ -337,13 +352,6 @@ public function getSettingsHtml (): string
337352 ]);
338353 }
339354
340- /**
341- * @param null $value
342- * @param ElementInterface|null $element
343- *
344- * @return string
345- * @throws InvalidConfigException
346- */
347355 public function getInputHtml ($ value = null , ElementInterface $ element = null ): string
348356 {
349357 if ($ element !== null && $ element ->hasEagerLoadedElements ($ this ->handle ))
@@ -355,39 +363,11 @@ public function getInputHtml ($value = null, ElementInterface $element = null):
355363 );
356364 }
357365
358- /**
359- * @inheritdoc
360- *
361- * @param mixed $value
362- * @param ElementInterface $element
363- *
364- * @return string
365- * @throws Exception
366- */
367366 public function getTableAttributeHtml (mixed $ value , ElementInterface $ element ): string
368367 {
369368 return $ this ->normalizeValue ($ value , $ element )->address ;
370369 }
371370
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- */
391371 public function isValueEmpty ($ value , ElementInterface $ element ): bool
392372 {
393373 return $ this ->normalizeValue ($ value )->isValueEmpty ();
@@ -417,9 +397,6 @@ public function getContentGqlMutationArgumentType (): Type|array
417397 // Methods: Events
418398 // -------------------------------------------------------------------------
419399
420- /**
421- * @inheritdoc
422- */
423400 public function beforeSave (bool $ isNew ): bool
424401 {
425402 $ this ->lat = (float ) $ this ->lat ;
@@ -432,13 +409,6 @@ public function beforeSave (bool $isNew): bool
432409 return parent ::beforeSave ($ isNew );
433410 }
434411
435- /**
436- * @param ElementInterface $element
437- * @param bool $isNew
438- *
439- * @return bool
440- * @throws InvalidFieldException
441- */
442412 public function beforeElementSave (ElementInterface $ element , bool $ isNew ): bool
443413 {
444414 if (!SimpleMap::getInstance ()->map ->validateField ($ this , $ element ))
@@ -447,20 +417,27 @@ public function beforeElementSave (ElementInterface $element, bool $isNew): bool
447417 return parent ::beforeElementSave ($ element , $ isNew );
448418 }
449419
450- /**
451- * @param ElementInterface $element
452- * @param bool $isNew
453- *
454- * @throws InvalidFieldException
455- * @throws Throwable
456- */
457420 public function afterElementSave (ElementInterface $ element , bool $ isNew ): void
458421 {
459422 SimpleMap::getInstance ()->map ->saveField ($ this , $ element );
460423
461424 parent ::afterElementSave ($ element , $ isNew );
462425 }
463426
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+
464441 // Helpers
465442 // =========================================================================
466443
0 commit comments