Skip to content

Commit

Permalink
Improved support for collections
Browse files Browse the repository at this point in the history
  • Loading branch information
lindseydiloreto committed Dec 19, 2023
1 parent a48b734 commit 15df40a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

### Changed
- Improved support for [collections](https://craftcms.com/docs/4.x/element-queries.html#collect).
- Restores a required minimum of Craft 4.0.0.

## 4.3.9 - 2023-12-18
Expand Down
2 changes: 1 addition & 1 deletion docs/dynamic-maps/locations.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,5 @@ $locations = Entry::find()->collect();
:::

:::tip Elements must have a valid Address field
When using an Element, ensure that it has at least one Address field attached to it. The plugin will automatically render all valid addresses on the map.
When using an Element, ensure that it has at least one Address field attached to it. The plugin will automatically render each valid Address field on the map.
:::
10 changes: 5 additions & 5 deletions src/helpers/GoogleMaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

use Craft;
use craft\base\Element;
use craft\elements\ElementCollection;
use doublesecretagency\googlemaps\GoogleMapsPlugin;
use doublesecretagency\googlemaps\models\DynamicMap;
use doublesecretagency\googlemaps\models\Location;
use doublesecretagency\googlemaps\models\Lookup;
use doublesecretagency\googlemaps\models\StaticMap;
use doublesecretagency\googlemaps\models\Visitor;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Collection;
use yii\base\Exception;
use yii\base\InvalidConfigException;

Expand Down Expand Up @@ -98,11 +98,11 @@ public static function loadAssets(array $params = []): void
/**
* Create a new Dynamic Map object.
*
* @param array|ElementCollection|Element|Location $locations
* @param array|Collection|Element|Location $locations
* @param array $options
* @return DynamicMap
*/
public static function map(array|ElementCollection|Element|Location $locations = [], array $options = []): DynamicMap
public static function map(array|Collection|Element|Location $locations = [], array $options = []): DynamicMap
{
// Create a new map object
$map = new DynamicMap($locations, $options);
Expand Down Expand Up @@ -143,11 +143,11 @@ public static function getMap(string $mapId): ?DynamicMap
/**
* Create a new Static Map object.
*
* @param array|ElementCollection|Element|Location $locations
* @param array|Collection|Element|Location $locations
* @param array $options
* @return StaticMap
*/
public static function img(array|ElementCollection|Element|Location $locations = [], array $options = []): StaticMap
public static function img(array|Collection|Element|Location $locations = [], array $options = []): StaticMap
{
return new StaticMap($locations, $options);
}
Expand Down
10 changes: 5 additions & 5 deletions src/helpers/MapHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

use Craft;
use craft\base\Element;
use craft\elements\ElementCollection;
use craft\helpers\StringHelper;
use craft\models\FieldLayout;
use doublesecretagency\googlemaps\fields\AddressField;
use doublesecretagency\googlemaps\models\Address;
use doublesecretagency\googlemaps\models\Location;
use Illuminate\Support\Collection;

/**
* Class MapHelper
Expand Down Expand Up @@ -50,11 +50,11 @@ public static function generateId(?string $prefix = null): string
* Coordinates will always be returned inside a parent array,
* to compensate for Elements with multiple Address Fields.
*
* @param array|ElementCollection|Element|Location $locations
* @param array|Collection|Element|Location $locations
* @param array $options
* @return array Collection of coordinate sets
*/
public static function extractCoords(array|ElementCollection|Element|Location $locations, array $options = []): array
public static function extractCoords(array|Collection|Element|Location $locations, array $options = []): array
{
// If it's an Address Model, return the coordinates w/ optional ID
if ($locations instanceof Address) {
Expand Down Expand Up @@ -98,8 +98,8 @@ public static function extractCoords(array|ElementCollection|Element|Location $l
return [$locations];
}

// If it's an Element Collection
if ($locations instanceof ElementCollection) {
// If it's a Collection
if ($locations instanceof Collection) {
// Convert to an array
$locations = $locations->all();
}
Expand Down
26 changes: 13 additions & 13 deletions src/models/DynamicMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Craft;
use craft\base\Element;
use craft\base\Model;
use craft\elements\ElementCollection;
use craft\helpers\Html;
use craft\helpers\Json;
use craft\helpers\StringHelper;
Expand All @@ -26,6 +25,7 @@
use doublesecretagency\googlemaps\GoogleMapsPlugin;
use doublesecretagency\googlemaps\helpers\GoogleMaps;
use doublesecretagency\googlemaps\helpers\MapHelper;
use Illuminate\Support\Collection;
use Throwable;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
Expand Down Expand Up @@ -82,15 +82,15 @@ public function __toString(): string
/**
* Initialize a Dynamic Map object.
*
* @param array|ElementCollection|Element|Location $locations
* @param array|Collection|Element|Location $locations
* @param array $options
* @param array $config
* @throws Exception
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function __construct(array|ElementCollection|Element|Location $locations = [], array $options = [], array $config = [])
public function __construct(array|Collection|Element|Location $locations = [], array $options = [], array $config = [])
{
// Call parent constructor
parent::__construct($config);
Expand Down Expand Up @@ -134,15 +134,15 @@ public function __construct(array|ElementCollection|Element|Location $locations
/**
* Add one or more markers to the map.
*
* @param array|ElementCollection|Element|Location $locations
* @param array|Collection|Element|Location $locations
* @param array $options
* @return $this
* @throws Exception
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function markers(array|ElementCollection|Element|Location $locations, array $options = []): DynamicMap
public function markers(array|Collection|Element|Location $locations, array $options = []): DynamicMap
{
// If no locations were specified, bail
if (!$locations) {
Expand Down Expand Up @@ -176,11 +176,11 @@ public function markers(array|ElementCollection|Element|Location $locations, arr
/**
* Add one or more circles to the map.
*
* @param array|ElementCollection|Element|Location $locations
* @param array|Collection|Element|Location $locations
* @param array $options
* @return $this
*/
public function circles(array|ElementCollection|Element|Location $locations, array $options = []): DynamicMap
public function circles(array|Collection|Element|Location $locations, array $options = []): DynamicMap
{
// If no locations were specified, bail
if (!$locations) {
Expand Down Expand Up @@ -610,14 +610,14 @@ public function getDna(): array
/**
* Create individual markers one at a time.
*
* @param array|ElementCollection|Element|Location $locations
* @param array|Collection|Element|Location $locations
* @param array $options
* @throws Exception
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
private function _individualMarkers(array|ElementCollection|Element|Location $locations, array $options): void
private function _individualMarkers(array|Collection|Element|Location $locations, array $options): void
{
// Initialize infoWindowOptions
$options = $options ?? [];
Expand Down Expand Up @@ -710,14 +710,14 @@ private function _markerClick(string $markerId, array $options): void
/**
* Parse a dynamic marker string.
*
* @param array|ElementCollection|Element|Location $location
* @param array|Collection|Element|Location $location
* @param string $string
* @throws Exception
* @throws LoaderError
* @throws SyntaxError
* @throws Throwable
*/
private function _parseLocationString(array|ElementCollection|Element|Location $location, string &$string): void
private function _parseLocationString(array|Collection|Element|Location $location, string &$string): void
{
// Get view services
$view = Craft::$app->getView();
Expand All @@ -738,11 +738,11 @@ private function _parseLocationString(array|ElementCollection|Element|Location $
/**
* Creates a single marker with a corresponding info window.
*
* @param array|ElementCollection|Element|Location $location
* @param array|Collection|Element|Location $location
* @param array $options
* @param bool $isCoords
*/
private function _markerInfoWindow(array|ElementCollection|Element|Location $location, array &$options, bool $isCoords): void
private function _markerInfoWindow(array|Collection|Element|Location $location, array &$options, bool $isCoords): void
{
// Initialize marker data
$infoWindow = [
Expand Down
14 changes: 7 additions & 7 deletions src/models/StaticMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

use craft\base\Element;
use craft\base\Model;
use craft\elements\ElementCollection;
use craft\helpers\Html;
use craft\helpers\Template;
use doublesecretagency\googlemaps\helpers\GoogleMaps;
use doublesecretagency\googlemaps\helpers\MapHelper;
use Exception;
use Illuminate\Support\Collection;
use Twig\Markup;

/**
Expand Down Expand Up @@ -70,11 +70,11 @@ public function __toString(): string
/**
* Initialize a Static Map object.
*
* @param array|ElementCollection|Element|Location $locations
* @param array|Collection|Element|Location $locations
* @param array $options
* @param array $config
*/
public function __construct(array|ElementCollection|Element|Location $locations = [], array $options = [], array $config = [])
public function __construct(array|Collection|Element|Location $locations = [], array $options = [], array $config = [])
{
// Ensure options are a valid array
if (!$options || !is_array($options)) {
Expand Down Expand Up @@ -108,11 +108,11 @@ public function __construct(array|ElementCollection|Element|Location $locations
/**
* Add one or more markers to the map.
*
* @param array|ElementCollection|Element|Location $locations
* @param array|Collection|Element|Location $locations
* @param array $options
* @return $this
*/
public function markers(array|ElementCollection|Element|Location $locations, array $options = []): StaticMap
public function markers(array|Collection|Element|Location $locations, array $options = []): StaticMap
{
// If no locations were specified, bail
if (!$locations) {
Expand Down Expand Up @@ -166,11 +166,11 @@ public function markers(array|ElementCollection|Element|Location $locations, arr
/**
* Add a defined path to the map.
*
* @param array|ElementCollection|Element|Location $points
* @param array|Collection|Element|Location $points
* @param array $options
* @return $this
*/
public function path(array|ElementCollection|Element|Location $points, array $options = []): StaticMap
public function path(array|Collection|Element|Location $points, array $options = []): StaticMap
{
// If no points were specified, bail
if (!$points) {
Expand Down

0 comments on commit 15df40a

Please sign in to comment.