Is it possible for a plugin to use the Address Field like Commerce v4 does in Store Settings? #11173
-
I've tried to copy the code in the StoreController:
I can output the initial "Add an Address" UI but when I click on it I get an "User not authorized to create this element" error from the request to create the initial Address Element. On my first few passes, I don't see any way to change user permissions in the code. Is it possible for our third-party plugins to use the Address Field in the same way that Craft Commerce does? If so, what stars do we need to align to get it to work without errors? Any tips or nudges to get me moving in the right direction would be appreciated. As a side note, your now first-party Address Field relies on the same library that I was using in the Sprout Address field. I'm planning on retiring the Sprout Address field as it feels silly to recreate the wheel when you folks have it in core now. If this is not possible and there is anything I can do on my end to help make this possible, I would rather spend my time helping make this use case possible than migrating an alternative option that nobody should need in a few months. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
After a bit more digging, this appears possible. I can create the field like so, passing it an address Element:
Return the code generated for the
And add an input to identify the address field I'm managing within the form the address exists: {{ hiddenInput('locationAddressId', 7) }} To address the permission issues on save, you will need to add support for the And, as far as I can tell, the stuff in the StoreLocationBehavior is handled in the above event and the behavior class never gets attached or used. The Address Element gets stored when you save it from the Slideout editor. The "Label" field gets saved to |
Beta Was this translation helpful? Give feedback.
After a bit more digging, this appears possible.
I can create the field like so, passing it an address Element:
Return the code generated for the
addressCardsHtml
to the template and output it with the|raw
filter:And add an input to identify the address field I'm managing within the form the address exists:
{{ hiddenInput('locationAddressId', 7) }}
To address the permission issues on save, you will need to add support for the
Address::EVENT_AUTHORIZE_VIEW
andAddress::EVENT_AUTHORIZE_SAVE
events. See the exa…