{% block fourtwosix_phone_prefix %}
{% if config("FourtwosixPhoneValidation.config.active") %}
{% set fourtwosixExtension = page.extension('FourtwosixPhoneNumberCountryPrefix') ?? null %}
{% set ftsPhonePrefix = fourtwosixExtension["fts_prefixes"] %}
{# This can be address or page.address is passed as a parameter in vendor/shopware/storefront/Resources/views/storefront/component/address/address-editor-modal-create-address.html.twig #}
{% set ftsAddressId = page.address.id ?? address.id %}
{% set uuid = ftsAddressId ?? random() %}
<div class="form-group col-md-2">
<p class="form-label"
for="{{ idPrefix ~ prefix }}AddressPhoneNumberPrefix-{{ ftsAddressId }}">
{{ "fourwosixPhonePrefix.phoneNumberLabelPrefix"|trans|sw_sanitize }}
{{ "general.required"|trans|sw_sanitize }}
</p>
<select
{# added to the corresponding array billingAddress[] / shippinAddress[] / address #}
name="{{ prefix }}[phonePrefix]"
class="form-control prefix-from-iso"
id="{{ idPrefix ~ prefix }}AddressPhoneNumberPrefix-{{ ftsAddressId }}"
required="required"
data-address-id="{{ uuid }}"
>
<option
{# ftsPhonePrefix has this structure [[id => value]] so if no id found and no extension set is a registration / new address creation #}
{{ ftsPhonePrefix[ftsAddressId] ? "" : 'selected="true"' }}
disabled="disabled"
value>
{{ "fourwosixPhonePrefix.chooseCountry"|trans|sw_sanitize }}
</option>
{% for ftsCountries in fourtwosixExtension["countries"] %}
<option
{# In /register /overview/address (modal) the form violation is presented as an associative array with
[
'billingAddress/shippingAddress/address' => [
"phonePrefix" => "+49"
]
]
Therefore prepend the prefix to select the correct prefix in the dropdown
------------------------------------------------
In address/{address-id} there is no associative array
but is accessible directly
[
"phonePrefix" => "+49"
] #}
{% set formViolationPrefix = formViolations.inputData[idPrefix ~ prefix]["phonePrefix"] ?? formViolations.inputData["phonePrefix"] ?? false %}
{% if formViolationPrefix === ftsCountries["prefix"] %}
selected="selected"
{% else %}
{# Only if no violation are found we preload the data from the extension loaded via subscriber
this is for when the user makes a mistake eg. wrong zipcode but have also changed the prefix dropdown #}
{% if ftsPhonePrefix[ftsAddressId] === ftsCountries["prefix"] %}
selected="selected"
{% endif %}
{% endif %}
data-iso="{{ ftsCountries["iso"] }}"
value="{{ ftsCountries["prefix"] }}">
{{ ftsCountries["prefix"] }} {{ ftsCountries["iso"] }}
</option>
{% endfor %}
</select>
</div>
{% endif %}
{% endblock %}