Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #59 from SamNewism/master
Browse files Browse the repository at this point in the history
fix bug with switching country when address is used on a non-entry
  • Loading branch information
leevigraham authored Jul 31, 2020
2 parents 697d1d7 + 817daa8 commit 95478a6
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 26 deletions.
23 changes: 13 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,38 @@ 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/).

## 0.0.21 - 2020.07.31
### Fixes
- Fixed Issue #58 - https://github.com/newism/craft3-fields/issues/58

## 0.0.20 - 2020.07.23
### Fixes
- Fixed map loading when google hasn't loaded

## 0.0.19 - 2020.07.23
### Updated
- Added map + marker to Address field

## 0.0.18 - 2020.07.22
### Updated
- Google API Key Setting can now be set as an `$_ENV` variables
- Code clean up

## 0.0.17 - 2020.07.22
### Added
- New setting to hide Address Place Data
### Fixes
- Disabling AutoComplete breaks things! #43

## 0.0.16 - 2020.07.21
### Fixes
- Fixes Show Map Url field option
- Fixes Person Name field spacing - Added flex wrap and margins

## 0.0.15 - 2020.05.19
### Fixes
- Fixes Neo / Matrix integration

## 0.0.14 - 2019.01.15
### Fixes
- Fixes #30: Default Country Code doesn't do anything.
Expand All @@ -46,7 +49,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- Address uses a formatted string for search keywords
- Embed uses the embed title for search keywords
### Fixed
- Fixes #39 Adresse cannot contain emoji - Address and Embed now use `StringHelper::encodeMb4` to serialize their DB value
- Fixes #39 Adresse cannot contain emoji - Address and Embed now use `StringHelper::encodeMb4` to serialize their DB value

## 0.0.11 - 2018.08.21
### Added
Expand All @@ -59,15 +62,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
## 0.0.10 - 2018.1.03
### Fixed
- Fixes #22 Encode all emoticons in shortcodes using LitEmoji
- Craft 3 RC-13 compatibility
- Craft 3 RC-13 compatibility

## 0.0.8 - 2018.6.02
### Fixed
- Fixes #18 Telephone field causes a PHP error when entry is saved
- Fixes #18 Telephone field causes a PHP error when entry is saved

## 0.0.7 - 2017.12.08
### Fixed
- Fixes #15 Cannot read property 'serialize' of undefined - Address.js:88
- Fixes #15 Cannot read property 'serialize' of undefined - Address.js:88

## 0.0.6 - 2017.12.04
### Changed
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": "newism/craft3-fields",
"description": "Address, telephone and email fields for CraftCMS 3.x",
"type": "craft-plugin",
"version": "0.0.20",
"version": "0.0.21",
"keywords": [
"craft",
"cms",
Expand Down
15 changes: 8 additions & 7 deletions src/assetbundles/addressfield/dist/js/Address.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
Plugin.prototype = {

getElement: function (element) {
return this.$element.find('#' + this.options.namespace + '-' + element);
return this.$element.find('#' + this.options.namespacedId + '-' + element);
},

init: function (id) {
Expand Down Expand Up @@ -149,17 +149,18 @@
this.$spinner.removeClass('hidden');

jqXHR = Craft.postActionRequest(
'entries/switch-entry-type',
Craft.cp.$primaryForm.serialize(),
'nsm-fields/address/refresh-country',
{
'CRAFT_CSRF_TOKEN': Craft.cp.$primaryForm.find('[name="CRAFT_CSRF_TOKEN"]').val(),
'countryCode': this.currentCountryCode,
'namespace': this.options.namespace,
},
$.proxy(function (response, textStatus) {
var newHtml;
this.$spinner.addClass('hidden');
if (textStatus === 'success') {
newHtml = $(response.fieldsHtml).find('#' + this.options.namespace + '-field .nsmFields-address-addressFieldsContainer');
newHtml.toggle(!!this.$countryCodeInput.val());
var newHtml = $(response.html).find('.nsmFields-address-addressFieldsContainer');
this.$addressFieldsContainer.replaceWith(newHtml);
this.$addressFieldsContainer = newHtml;
Craft.initUiElements(this.$addressFieldsContainer);
}
}, this));

Expand Down
80 changes: 80 additions & 0 deletions src/controllers/AddressController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* NSM Fields for Craft CMS 3.x
*
* nsm-fields
*
* @link http://newism.com.au
* @copyright Copyright (c) 2017 newism
*/

namespace newism\fields\controllers;

use Craft;
use craft\web\Controller;
use Exception;
use newism\fields\NsmFields;
use Twig_Error_Loader;

use yii\web\Response;
use newism\fields\models\AddressModel;

/**
* Address Controller
*
* Generally speaking, controllers are the middlemen between the front end of
* the CP/website and your plugin’s services. They contain action methods which
* handle individual tasks.
*
* A common pattern used throughout Craft involves a controller action gathering
* post data, saving it on a model, passing the model off to a service, and then
* responding to the request appropriately depending on the service method’s response.
*
* Action methods begin with the prefix “action”, followed by a description of what
* the method does (for example, actionSaveIngredient()).
*
* https://craftcms.com/docs/plugins/controllers
*
* @author newism
* @package nsm-fields
* @since 1.0.0
*/
class AddressController extends Controller
{

// Protected Properties
// =========================================================================

/**
* @var bool|array Allows anonymous access to this controller's actions.
* The actions must be in 'kebab-case'
* @access protected
*/
protected $allowAnonymous = [];

// Public Methods
// =========================================================================

/**
* @return response
* @throws Twig_Error_Loader
* @throws \yii\base\Exception
*/
public function actionRefreshCountry(): Response
{
$this->requireAcceptsJson();

$address = Craft::$app->fields->getFieldByHandle('address');

$addressModel = new AddressModel([
'countryCode' => Craft::$app->request->post('countryCode')
]);

$response = [
'html' => Craft::$app->getView()->namespaceInputs($address->renderFormFields($addressModel), Craft::$app->request->post('namespace'))
];

return $this->asJson($response);
}

}
6 changes: 3 additions & 3 deletions src/controllers/EmbedController.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* test plugin for Craft CMS 3.x
* NSM Fields for Craft CMS 3.x
*
* test
* nsm-fields
*
* @link http://newism.com.au
* @copyright Copyright (c) 2017 newism
Expand Down Expand Up @@ -33,7 +33,7 @@
* https://craftcms.com/docs/plugins/controllers
*
* @author newism
* @package Test
* @package nsm-fields
* @since 1.0.0
*/
class EmbedController extends Controller
Expand Down
8 changes: 6 additions & 2 deletions src/fields/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ public function getInputHtml(
$jsonVars = [
'id' => $id,
'name' => $this->handle,
'namespace' => $namespacedId,
'namespace' => Craft::$app->getView()->getNamespace(),
'namespacedId' => $namespacedId,
'prefix' => Craft::$app->getView()->namespaceInputId(''),
'fieldSettings' => $fieldSettings,
'pluginSettings' => $pluginSettings,
Expand All @@ -209,7 +210,7 @@ public function getInputHtml(
* @throws Twig_Error_Loader
* @throws RuntimeException
*/
protected function renderFormFields(AddressModel $value = null)
public function renderFormFields(AddressModel $value = null)
{
// Get our id and namespace
$id = Craft::$app->getView()->formatInputId($this->handle);
Expand All @@ -229,6 +230,7 @@ protected function renderFormFields(AddressModel $value = null)
'value' => $countryCode,
'field' => $this,
'id' => $id,
'namespace' => Craft::$app->getView()->getNamespace(),
'namespacedId' => $namespacedId,
'settings' => $fieldSettings,
'countryOptions' => $this->getCountryOptions(),
Expand All @@ -245,6 +247,7 @@ protected function renderFormFields(AddressModel $value = null)
'value' => $value,
'field' => $this,
'id' => $id,
'namespace' => Craft::$app->getView()->getNamespace(),
'namespacedId' => $namespacedId,
'fieldSettings' => $fieldSettings,
'pluginSettings' => $pluginSettings,
Expand Down Expand Up @@ -337,6 +340,7 @@ private function renderAddressFields($value)
'value' => $value,
'field' => $this,
'id' => $id,
'namespace' => Craft::$app->getView()->getNamespace(),
'namespacedId' => $namespacedId,
'settings' => $fieldSettings,
'addressFormat' => $addressFormat,
Expand Down
6 changes: 3 additions & 3 deletions src/services/Embed.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* test plugin for Craft CMS 3.x
* NSM Fieldsfor Craft CMS 3.x
*
* test
* nsm-fields
*
* @link http://newism.com.au
* @copyright Copyright (c) 2017 Newism
Expand All @@ -22,7 +22,7 @@
* https://craftcms.com/docs/plugins/services
*
* @author Newism
* @package Test
* @package nsm-fields
* @since 1.0.0
*/
class Embed extends Component
Expand Down

0 comments on commit 95478a6

Please sign in to comment.