Skip to content

Commit

Permalink
Merge pull request #3574 from craftcms/feature/pt-1917-5x-custom-addr…
Browse files Browse the repository at this point in the history
…ess-fields-are-not-displayed-on-inventory

Fixed address custom fields not displaying on the inventory location edit page
nfourtythree authored Jul 10, 2024
2 parents 04af355 + 37eb64a commit a22ceaf
Showing 3 changed files with 79 additions and 49 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
- Fixed a bug where it was possible to select shipping and tax categories not associated to the current product type. ([#3557](https://github.com/craftcms/commerce/issues/3557))
- Fixed a bug where payment currencies weren’t deletable. ([#3548](https://github.com/craftcms/commerce/issues/3548))
- Fixed a bug where variant field layouts could show incorrectly. ([#3570](https://github.com/craftcms/commerce/issues/3570))
- Fixed a bug where address custom fields weren’t showing on the Edit Inventory Location page. ([#3569](https://github.com/craftcms/commerce/issues/3569))

## 5.0.11.1 - 2024-06-20

80 changes: 77 additions & 3 deletions src/controllers/InventoryLocationsController.php
Original file line number Diff line number Diff line change
@@ -15,6 +15,9 @@
use craft\errors\DeprecationException;
use craft\errors\ElementNotFoundException;
use craft\fieldlayoutelements\addresses\AddressField;
use craft\fieldlayoutelements\addresses\LabelField;
use craft\helpers\ArrayHelper;
use craft\helpers\Cp;
use craft\helpers\Html;
use craft\web\Controller;
use Throwable;
@@ -115,18 +118,82 @@ public function actionEdit(?int $inventoryLocationId = null, ?InventoryLocation
}
}

Craft::$app->getView()->setNamespace('inventoryLocationAddress');

$address = $inventoryLocation->getAddress();
$fieldLayout = $address->getFieldLayout();

$form = $fieldLayout->createForm($address);
$form->tabIdPrefix = 'inventoryLocationAddress';
$tabs = $form->getTabMenu();
// Reset the `tabIdPrefix` so that the namespaces are correct for the inventory location fields
$form->tabIdPrefix = null;

// Remove the title/label field from the address field layout
foreach ($form->tabs as &$tab) {
$tab->elements = array_filter($tab->elements, function($element) {
if (isset($element[0]) && $element[0] instanceof LabelField && $element[0]->attribute === 'title') {
return false;
}

return true;
});
}

ArrayHelper::prependOrAppend($form->tabs[0]->elements, [
null,
false,
Html::tag('hr'),
], true);
ArrayHelper::prependOrAppend($form->tabs[0]->elements, [
null,
false,
Html::hiddenInput('id', (string)$address->id),
], true);
ArrayHelper::prependOrAppend($form->tabs[0]->elements, [
null,
false,
Cp::textFieldHtml([
'name' => 'handle',
'id' => 'handle',
'value' => $inventoryLocation->handle,
'required' => true,
'label' => Craft::t('commerce', 'Handle'),
'errors' => $inventoryLocation->getErrors('handle'),
]),
], true);
ArrayHelper::prependOrAppend($form->tabs[0]->elements, [
null,
false,
Cp::textFieldHtml([
'name' => 'name',
'id' => 'name',
'value' => $inventoryLocation->name,
'required' => true,
'label' => Craft::t('commerce', 'Name'),
'errors' => $inventoryLocation->getErrors('name'),
]),
], true);
ArrayHelper::prependOrAppend($form->tabs[0]->elements, [
null,
false,
Html::hiddenInput('inventoryLocationId', (string)$inventoryLocationId),
], true);

$variables = [
'inventoryLocationId' => $inventoryLocationId,
'inventoryLocation' => $inventoryLocation,
'typeName' => Craft::t('commerce', 'Inventory Location'),
'lowerTypeName' => Craft::t('commerce', 'inventory location'),
'locationFieldHtml' => '',
'addressField' => new AddressField(),
'form' => $form,
'countries' => Craft::$app->getAddresses()->getCountryRepository()->getList(Craft::$app->language),
];

return $this->asCpScreen()
->title($title)
->tabs($tabs)
->addCrumb(Craft::t('app', 'Inventory Locations'), 'commerce/inventory-locations')
->action('commerce/inventory-locations/save')
->redirectUrl('commerce/inventory-locations')
@@ -148,7 +215,7 @@ public function actionSave(): ?Response
$this->requirePostRequest();

// find the inventory location or make a new one
$inventoryLocationId = Craft::$app->getRequest()->getBodyParam('inventoryLocationId');
$inventoryLocationId = Craft::$app->getRequest()->getBodyParam('inventoryLocationAddress[inventoryLocationId]');
$inventoryLocation = null;

if ($inventoryLocationId) {
@@ -159,14 +226,17 @@ public function actionSave(): ?Response
$inventoryLocation = new InventoryLocation();
}

$inventoryLocation->name = Craft::$app->getRequest()->getBodyParam('name');
$inventoryLocation->handle = Craft::$app->getRequest()->getBodyParam('handle');
$inventoryLocation->name = Craft::$app->getRequest()->getBodyParam('inventoryLocationAddress[name]');
$inventoryLocation->handle = Craft::$app->getRequest()->getBodyParam('inventoryLocationAddress[handle]');

// Pre-validate the inventory location so that we don't save the address if the rest isn't valid
// This is to avoid orphaned addresses
$isValid = $inventoryLocation->validate();

if ($inventoryLocationAddress = Craft::$app->getRequest()->getBodyParam('inventoryLocationAddress')) {
// Remove the non-address fields from the post data
unset($inventoryLocationAddress['name'], $inventoryLocationAddress['handle'], $inventoryLocationAddress['inventoryLocationId']);

$inventoryLocationAddress['title'] = $inventoryLocation->name;
if ($isValid) {
$addressId = $inventoryLocationAddress['id'] ?: null;
@@ -179,6 +249,10 @@ public function actionSave(): ?Response

$address->setAttributes($inventoryLocationAddress, false);

if (isset($inventoryLocationAddress['fields'])) {
$address->setFieldValues($inventoryLocationAddress['fields']);
}

// Only try and save if the inventory location is valid
$hasAddressErrors = false;
if ($isValid && !Craft::$app->getElements()->saveElement($address)) {
47 changes: 1 addition & 46 deletions src/templates/inventory-locations/_edit.twig
Original file line number Diff line number Diff line change
@@ -1,50 +1,5 @@
{% import "_includes/forms" as forms %}
{% set address = inventoryLocation.getAddress() %}

{{ forms.textField({
first: true,
label: "Name"|t('commerce'),
id: 'name',
name: 'name',
value: inventoryLocation.name,
errors: inventoryLocation.getErrors('name'),
autofocus: true,
required: true,
}) }}

{{ forms.textField({
label: "Handle"|t('commerce'),
id: 'handle',
name: 'handle',
value: inventoryLocation.handle,
errors: inventoryLocation.getErrors('handle'),
required: true,
}) }}

{{ forms.hidden({
name: 'inventoryLocationId',
value: inventoryLocation.id ?? ''
}) }}

{% namespace 'inventoryLocationAddress' %}
{% set addressFields = addressField.formHtml(address) %}

<hr>
<h2>{{ 'Address'|t('commerce') }}</h2>
{{ hiddenInput('id', address.id) }}

<div class="flex flex-nowrap">
{{ forms.selectizeField({
id: 'countryCode',
name: 'countryCode',
options: countries,
value: address.countryCode,
autocomplete: 'off',
}) }}
<div id="countryCode-spinner" class="spinner hidden"></div>
</div>

{{ addressFields|raw }}
{{ form.render()|raw }}
{% endnamespace %}

{% hook "cp.commerce.inventoryLocation.edit" %}

0 comments on commit a22ceaf

Please sign in to comment.