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

Commit

Permalink
fix bug with switching country when address is used on a non-entry
Browse files Browse the repository at this point in the history
  • Loading branch information
SamNewism committed Jul 31, 2020
1 parent 697d1d7 commit 049af36
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 9 deletions.
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
/**
* test plugin for Craft CMS 3.x
*
* test
*
* @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 Test
* @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);
}

}
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

0 comments on commit 049af36

Please sign in to comment.