Skip to content

Commit

Permalink
Implements PreviewableFieldInterface for Location fields. Bump to 3.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mmikkel committed Feb 15, 2023
1 parent 1baeee2 commit 1108470
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 3.1.0 - 2023-02-15
### Added
- Location fields are now previewable in element indexes

## 3.0.2 - 2023-02-07

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vaersaagod/locate",
"description": "Harness the power of the Google Autocomplete API inside Craft. Adds an autocomplete search box to Craft entries.",
"type": "craft-plugin",
"version": "3.0.2",
"version": "3.1.0",
"keywords": [
"craft",
"cms",
Expand Down
49 changes: 33 additions & 16 deletions src/fields/LocateField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@

namespace vaersaagod\locate\fields;

use vaersaagod\locate\Locate;
use vaersaagod\locate\assetbundles\locatefield\LocateFieldAsset;
use vaersaagod\locate\models\LocateModel;

use Craft;
use craft\base\ElementInterface;
use craft\base\Field;
use craft\elements\db\ElementQueryInterface;
use craft\helpers\App;
use craft\helpers\Db;
use craft\helpers\Html;
use craft\helpers\Json;
use craft\base\PreviewableFieldInterface;
use craft\helpers\StringHelper;
use craft\helpers\UrlHelper;

use vaersaagod\locate\Locate;
use vaersaagod\locate\assetbundles\locatefield\LocateFieldAsset;
use vaersaagod\locate\models\LocateModel;

use yii\db\Schema;

/**
Expand All @@ -34,7 +33,7 @@
* @package Locate
* @since 2.0.0
*/
class LocateField extends Field
class LocateField extends Field implements PreviewableFieldInterface
{
// Public Properties
// =========================================================================
Expand Down Expand Up @@ -62,11 +61,10 @@ public static function displayName(): string
public function rules(): array
{
$rules = parent::rules();
$rules = array_merge($rules, [
return array_merge($rules, [
['optionsObject', 'string'],
['optionsObject', 'default', 'value' => ''],
]);
return $rules;
}

/** @inheritdoc */
Expand All @@ -86,11 +84,11 @@ public function normalizeValue(mixed $value, ?ElementInterface $element = null):

if (is_string($value)) {
$attr += array_filter(json_decode($value, true) ?: [],
function ($key) {
static function($key) {
return in_array($key, ['lat', 'lng', 'location', 'placeid', 'locationData']);
}, ARRAY_FILTER_USE_KEY);
} else if (is_array($value) && isset($value['isCpFormData'])) {
if (!array_key_exists('location', $value) || $value['location'] === '') {
if (!array_key_exists('location', $value) || $value['location'] === '') {
return new LocateModel();
}
$attr += [
Expand Down Expand Up @@ -126,7 +124,16 @@ public function getSettingsHtml(): ?string
);
}

/** @inheritdoc */
/**
* @param mixed $value
* @param ElementInterface|null $element
* @return string
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
* @throws \yii\base\Exception
* @throws \yii\base\InvalidConfigException
*/
public function getInputHtml(mixed $value, ?ElementInterface $element = null): string
{
if (!$value) {
Expand Down Expand Up @@ -181,8 +188,8 @@ public function getInputHtml(mixed $value, ?ElementInterface $element = null): s
Craft::$app->getView()->registerJsFile($apiUrl);

$js = <<<JS
$('#$namespacedId-field').LocateField($jsonVars);
JS;
$('#$namespacedId-field').LocateField($jsonVars);
JS;
Craft::$app->getView()->registerJs($js);
}

Expand All @@ -198,6 +205,15 @@ public function getInputHtml(mixed $value, ?ElementInterface $element = null): s
);
}

/** @inheritdoc */
public function getTableAttributeHtml(mixed $value, ElementInterface $element): string
{
if (!$value instanceof LocateModel) {
return '';
}
return $value->location;
}

/**
* @param array|null $data
* @return array
Expand All @@ -216,7 +232,9 @@ private function formatLocationData(?array $data = null): array
foreach ($addressComponents as $component) {
$type = $component['types'][0];

if (!$type) continue;
if (!$type) {
continue;
}

$components[$type] = $component['long_name'];
$components[$type . "_short"] = $component['short_name'];
Expand All @@ -225,6 +243,5 @@ private function formatLocationData(?array $data = null): array
$returnData['components'] = $components;

return $returnData;

}
}

0 comments on commit 1108470

Please sign in to comment.