public static function Country::processCountry in Address 8
Processes the address_country form element.
Parameters
array $element: The form element to process.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
array $complete_form: The complete form structure.
Return value
array The processed element.
Throws
\InvalidArgumentException Thrown when #available_countries is malformed.
File
- src/
Element/ Country.php, line 60
Class
- Country
- Provides a country form element.
Namespace
Drupal\address\ElementCode
public static function processCountry(array &$element, FormStateInterface $form_state, array &$complete_form) {
if (isset($element['#available_countries']) && !is_array($element['#available_countries'])) {
throw new \InvalidArgumentException('The #available_countries property must be an array.');
}
$full_country_list = \Drupal::service('address.country_repository')
->getList();
$country_list = $full_country_list;
if (!empty($element['#available_countries'])) {
$available_countries = $element['#available_countries'];
if (!empty($element['#default_value'])) {
// The current country should always be available.
$available_countries[] = $element['#default_value'];
}
$available_countries = array_combine($available_countries, $available_countries);
$country_list = array_intersect_key($country_list, $available_countries);
}
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.
$element['#default_value'] = key($country_list);
}
$element['#tree'] = TRUE;
// Hide the dropdown when there is only one possible value.
if (count($country_list) == 1 && $element['#required']) {
$element['country_code'] = [
'#type' => 'hidden',
'#value' => key($available_countries),
];
}
else {
$element['country_code'] = [
'#type' => 'select',
'#title' => $element['#title'],
'#title_display' => $element['#title_display'],
'#description_display' => $element['#description_display'],
'#options' => $country_list,
'#default_value' => $element['#default_value'],
'#required' => $element['#required'],
'#limit_validation_errors' => [],
'#attributes' => [
'class' => [
'country',
],
'autocomplete' => 'country',
],
'#weight' => -100,
];
if (!$element['#required']) {
$element['country_code']['#empty_value'] = '';
}
if (!empty($element['#ajax'])) {
$element['country_code']['#ajax'] = $element['#ajax'];
unset($element['#ajax']);
}
if (!empty($element['#description'])) {
$element['country_code']['#description'] = $element['#description'];
unset($element['#description']);
}
}
// Remove the 'country_code' level from form state values.
$element['country_code']['#parents'] = $element['#parents'];
return $element;
}