Commit e7ed929d authored by bojanz's avatar bojanz
Browse files

Issue #2727777 followup: Use the address_country form element in the...

Issue #2727777 followup: Use the address_country form element in the zone_territory form element. Fix bug with default countries.
parent c6448794
......@@ -122,10 +122,13 @@ class Address extends FormElement {
if (isset($element['#used_fields']) && !is_array($element['#used_fields'])) {
throw new \InvalidArgumentException('The #used_fields property must be an array.');
}
$id_prefix = implode('-', $element['#parents']);
$wrapper_id = Html::getUniqueId($id_prefix . '-ajax-wrapper');
$value = $element['#value'];
if (empty($value['country_code']) && $element['#required']) {
// Preselect the default country so that the other elements can be shown.
$value['country_code'] = Country::getDefaultCountry($element['#available_countries']);
}
$element = [
'#tree' => TRUE,
......
......@@ -73,12 +73,10 @@ class Country extends FormElement {
$available_countries = array_combine($available_countries, $available_countries);
$country_list = array_intersect_key($country_list, $available_countries);
}
$value = $element['#value'];
if (empty($value) && $element['#required']) {
if (empty($element['#default_value']) && $element['#required']) {
// Fallback to the first country in the list if the default country
// is empty even though the field is required.
$value = key($country_list);
$element['#default_value'] = key($country_list);
}
$element['#tree'] = TRUE;
......@@ -94,7 +92,7 @@ class Country extends FormElement {
'#type' => 'select',
'#title' => $element['#title'],
'#options' => $country_list,
'#default_value' => $value,
'#default_value' => $element['#default_value'],
'#required' => $element['#required'],
'#limit_validation_errors' => [],
'#attributes' => [
......@@ -117,4 +115,27 @@ class Country extends FormElement {
return $element;
}
/**
* Gets the default country based on the available countries.
*
* Used as a helper by parent form elements (Address, ZoneTerritory).
*
* @param array $available_countries
* The available countries, an array of country codes.
*
* @return string
* The default country.
*/
public static function getDefaultCountry(array $available_countries = []) {
$full_country_list = \Drupal::service('address.country_repository')->getList();
$country_list = $full_country_list;
if (!empty($available_countries)) {
$available_countries = array_combine($available_countries, $available_countries);
$country_list = array_intersect_key($country_list, $available_countries);
}
$default_country = key($country_list);
return $default_country;
}
}
......@@ -38,6 +38,9 @@ class ZoneTerritory extends FormElement {
public function getInfo() {
$class = get_class($this);
return [
// List of country codes. If empty, all countries will be available.
'#available_countries' => [],
'#input' => TRUE,
'#multiple' => FALSE,
'#default_value' => NULL,
......@@ -63,26 +66,27 @@ class ZoneTerritory extends FormElement {
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if (is_array($input)) {
return $input;
$value = $input;
}
else {
if (!is_array($element['#default_value'])) {
$element['#default_value'] = [];
}
// Initialize properties.
$properties = [
'country_code',
'administrative_area', 'locality', 'dependent_locality',
'included_postal_codes', 'excluded_postal_codes',
];
foreach ($properties as $property) {
if (!isset($element['#default_value'][$property])) {
$element['#default_value'][$property] = NULL;
}
$value = $element['#default_value'];
}
// Initialize properties.
$properties = [
'country_code',
'administrative_area', 'locality', 'dependent_locality',
'included_postal_codes', 'excluded_postal_codes',
];
foreach ($properties as $property) {
if (!isset($value[$property])) {
$value[$property] = NULL;
}
return $element['#default_value'];
}
return $value;
}
/**
......@@ -104,12 +108,10 @@ class ZoneTerritory extends FormElement {
public static function processTerritory(array &$element, FormStateInterface $form_state, array &$complete_form) {
$id_prefix = implode('-', $element['#parents']);
$wrapper_id = Html::getUniqueId($id_prefix . '-ajax-wrapper');
$country_list = \Drupal::service('address.country_repository')->getList();
$value = $element['#value'];
if (empty($value['country_code']) && $element['#required']) {
// Fallback to the first country in the list if the default country
// is empty even though the field is required.
$value['country_code'] = key($country_list);
// Preselect the default country so that the other elements can be shown.
$value['country_code'] = Country::getDefaultCountry($element['#available_countries']);
}
$element = [
......@@ -120,9 +122,9 @@ class ZoneTerritory extends FormElement {
'#wrapper_id' => $wrapper_id,
] + $element;
$element['country_code'] = [
'#type' => 'select',
'#type' => 'address_country',
'#title' => t('Country'),
'#options' => $country_list,
'#available_countries' => $element['#available_countries'],
'#default_value' => $value['country_code'],
'#required' => $element['#required'],
'#limit_validation_errors' => [],
......@@ -280,7 +282,7 @@ class ZoneTerritory extends FormElement {
*/
public static function ajaxRefresh(array $form, FormStateInterface $form_state) {
$country_element = $form_state->getTriggeringElement();
$address_element = NestedArray::getValue($form, array_slice($country_element['#array_parents'], 0, -1));
$address_element = NestedArray::getValue($form, array_slice($country_element['#array_parents'], 0, -2));
return $address_element;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment