Commit 12dcc9f4 authored by bojanz's avatar bojanz
Browse files

Add a widget setting for choosing the available countries.

parent 00748f34
......@@ -46,3 +46,13 @@ field.field_settings.address:
label: 'Used fields'
sequence:
- type: string
field.widget.settings.address_default:
type: mapping
label: 'Default address formatter settings'
mapping:
available_countries:
type: sequence
label: 'Available countries'
sequence:
- type: string
......@@ -115,6 +115,33 @@ class AddressDefaultWidget extends WidgetBase implements ContainerFactoryPluginI
);
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'available_countries' => [],
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = [];
$element['available_countries'] = [
'#type' => 'select',
'#title' => $this->t('Available countries'),
'#description' => $this->t('If no countries are selected, all countries will be available.'),
'#options' => $this->countryRepository->getList(),
'#default_value' => $this->getSetting('available_countries'),
'#multiple' => TRUE,
'#size' => 10,
];
return $element;
}
/**
* {@inheritdoc}
*/
......@@ -130,6 +157,15 @@ class AddressDefaultWidget extends WidgetBase implements ContainerFactoryPluginI
if (!$hasInput) {
$values = $items[$delta]->toArray();
}
// Prepare the filtered country list.
$countryList = $this->countryRepository->getList();
$availableCountries = array_filter($this->getSetting('available_countries'));
if (empty($availableCountries)) {
$countries = $countryList;
}
else {
$countries = array_intersect_key($countryList, $availableCountries);
}
$element += [
'#type' => 'details',
......@@ -151,7 +187,7 @@ class AddressDefaultWidget extends WidgetBase implements ContainerFactoryPluginI
$element['country_code'] = [
'#type' => 'select',
'#title' => $this->t('Country'),
'#options' => $this->countryRepository->getList(),
'#options' => $countries,
'#default_value' => $values['country_code'],
'#empty_value' => '',
'#limit_validation_errors' => [],
......
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