Commit 034924a2 authored by bojanz's avatar bojanz
Browse files

Issue #2908575 followup: Apply the same fix to ZoneTerritory.

parent 251c1460
......@@ -62,19 +62,15 @@ class ZoneTerritory extends FormElement {
}
/**
* {@inheritdoc}
* Ensures all keys are set on the provided value.
*
* @param array $value
* The value.
*
* @return array
* The modified value.
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if (is_array($input)) {
$value = $input;
}
else {
if (!is_array($element['#default_value'])) {
$element['#default_value'] = [];
}
$value = $element['#default_value'];
}
// Initialize properties.
public static function applyDefaults(array $value) {
$properties = [
'country_code',
'administrative_area', 'locality', 'dependent_locality',
......@@ -89,6 +85,24 @@ class ZoneTerritory extends FormElement {
return $value;
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
// Ensure both the default value and the input have all keys set.
// Preselect the default country to ensure it's present in the value.
if (is_array($input)) {
$input = self::applyDefaults($input);
}
$element['#default_value'] = (array) $element['#default_value'];
$element['#default_value'] = self::applyDefaults($element['#default_value']);
if (empty($element['#default_value']['country_code']) && $element['#required']) {
$element['#default_value']['country_code'] = Country::getDefaultCountry($element['#available_countries']);
}
return is_array($input) ? $input : $element['#default_value'];
}
/**
* Processes the zone territory form element.
*
......@@ -108,11 +122,8 @@ 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');
// The #value has the new values on #ajax, the #default_value otherwise.
$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,
......@@ -125,7 +136,7 @@ class ZoneTerritory extends FormElement {
'#type' => 'address_country',
'#title' => t('Country'),
'#available_countries' => $element['#available_countries'],
'#default_value' => $value['country_code'],
'#default_value' => $element['#default_value']['country_code'],
'#required' => $element['#required'],
'#limit_validation_errors' => [],
'#ajax' => [
......
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