Created
January 1, 2025 16:46
-
-
Save junaidpv/75ac3aff6d94cd58a736cd49950693bb to your computer and use it in GitHub Desktop.
Re-roll of https://www.drupal.org/project/address/issues/2626982#comment-15382818 to apply on 2.0.2 . This would not be required once we started to use 2.1.x releases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/config/schema/address.schema.yml b/config/schema/address.schema.yml | |
index e706c72..a9643b8 100644 | |
--- a/config/schema/address.schema.yml | |
+++ b/config/schema/address.schema.yml | |
@@ -151,6 +151,11 @@ field.widget.settings.address_default: | |
wrapper_type: | |
type: string | |
label: Wrapper type | |
+ preferred_countries: | |
+ type: sequence | |
+ label: 'Preferred countries' | |
+ sequence: | |
+ type: string | |
field.widget.settings.address_zone_default: | |
type: mapping | |
@@ -183,6 +188,12 @@ views.field.subdivision: | |
views.filter.country: | |
type: views.filter.in_operator | |
label: 'Country' | |
+ mapping: | |
+ preferred_countries: | |
+ type: sequence | |
+ label: 'Preferred countries' | |
+ sequence: | |
+ type: string | |
views.filter.country_code: | |
type: views.filter.country | |
diff --git a/src/Element/Address.php b/src/Element/Address.php | |
index 9d68f3c..ae4e9d8 100644 | |
--- a/src/Element/Address.php | |
+++ b/src/Element/Address.php | |
@@ -43,6 +43,7 @@ use Drupal\Core\Render\Element\FormElement; | |
* AddressField::POSTAL_CODE => FieldOverride::OPTIONAL, | |
* ], | |
* '#available_countries' => ['DE', 'FR'], | |
+ * '#preferred_countries' => ['DE'], | |
* ]; | |
* @endcode | |
* | |
@@ -60,6 +61,8 @@ class Address extends FormElement { | |
'#available_countries' => [], | |
// FieldOverride constants keyed by AddressField constants. | |
'#field_overrides' => [], | |
+ // List of country codes. Listed countries will be at the top of select. | |
+ '#preferred_countries' => [], | |
'#input' => TRUE, | |
'#multiple' => FALSE, | |
@@ -177,6 +180,7 @@ class Address extends FormElement { | |
'#type' => 'address_country', | |
'#title' => t('Country'), | |
'#available_countries' => $element['#available_countries'], | |
+ '#preferred_countries' => $element['#preferred_countries'], | |
'#default_value' => $element['#default_value']['country_code'], | |
'#required' => $element['#required'], | |
'#limit_validation_errors' => [], | |
diff --git a/src/Element/Country.php b/src/Element/Country.php | |
index 145f46f..96d85a4 100644 | |
--- a/src/Element/Country.php | |
+++ b/src/Element/Country.php | |
@@ -2,6 +2,7 @@ | |
namespace Drupal\address\Element; | |
+use Drupal\address\PreferredCountriesTrait; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\Core\Render\Element\FormElement; | |
@@ -14,6 +15,7 @@ use Drupal\Core\Render\Element\FormElement; | |
* '#type' => 'address_country', | |
* '#default_value' => 'DE', | |
* '#available_countries' => ['DE', 'FR'], | |
+ * '#preferred_countries' => ['DE'], | |
* ]; | |
* @endcode | |
* | |
@@ -21,6 +23,8 @@ use Drupal\Core\Render\Element\FormElement; | |
*/ | |
class Country extends FormElement { | |
+ use PreferredCountriesTrait; | |
+ | |
/** | |
* {@inheritdoc} | |
*/ | |
@@ -29,6 +33,8 @@ class Country extends FormElement { | |
return [ | |
// List of country codes. If empty, all countries will be available. | |
'#available_countries' => [], | |
+ // List of country codes. Listed countries will be at the top of select. | |
+ '#preferred_countries' => [], | |
'#input' => TRUE, | |
'#multiple' => FALSE, | |
@@ -81,6 +87,14 @@ class Country extends FormElement { | |
} | |
$element['#tree'] = TRUE; | |
+ | |
+ // Rebuild country options if any preferred countries have been selected. | |
+ $preferred_countries = $element['#preferred_countries'] ?? []; | |
+ $preferred_countries = array_combine($preferred_countries, $preferred_countries); | |
+ $country_options = empty($preferred_countries) | |
+ ? $country_list | |
+ : self::rebuildCountryList($country_list, $preferred_countries); | |
+ | |
// Hide the dropdown when there is only one possible value. | |
if (count($country_list) == 1 && $element['#required']) { | |
$element['country_code'] = [ | |
@@ -94,7 +108,7 @@ class Country extends FormElement { | |
'#title' => $element['#title'], | |
'#title_display' => $element['#title_display'], | |
'#description_display' => $element['#description_display'], | |
- '#options' => $country_list, | |
+ '#options' => $country_options, | |
'#default_value' => $element['#default_value'], | |
'#required' => $element['#required'], | |
'#disabled' => $element['#disabled'], | |
diff --git a/src/Plugin/Field/FieldWidget/AddressDefaultWidget.php b/src/Plugin/Field/FieldWidget/AddressDefaultWidget.php | |
index 7cc303d..dfe2149 100644 | |
--- a/src/Plugin/Field/FieldWidget/AddressDefaultWidget.php | |
+++ b/src/Plugin/Field/FieldWidget/AddressDefaultWidget.php | |
@@ -94,11 +94,12 @@ class AddressDefaultWidget extends WidgetBase implements ContainerFactoryPluginI | |
); | |
} | |
- /** | |
+ /** | |
* {@inheritdoc} | |
*/ | |
public static function defaultSettings() { | |
return [ | |
+ 'preferred_countries' => [], | |
'wrapper_type' => 'fieldset', | |
] + parent::defaultSettings(); | |
} | |
@@ -107,14 +108,32 @@ class AddressDefaultWidget extends WidgetBase implements ContainerFactoryPluginI | |
* {@inheritdoc} | |
*/ | |
public function settingsForm(array $form, FormStateInterface $form_state) { | |
- return [ | |
- 'wrapper_type' => [ | |
- '#type' => 'radios', | |
- '#title' => $this->t('Wrapper type'), | |
- '#options' => $this->wrapperTypeOptions(), | |
- '#default_value' => $this->getSetting('wrapper_type'), | |
- ], | |
+ $element = []; | |
+ | |
+ $country_list = $this->countryRepository->getList(); | |
+ $available_countries = $this->getFieldSetting('available_countries'); | |
+ if (!empty($available_countries)) { | |
+ $country_list = array_intersect_key($country_list, $available_countries); | |
+ } | |
+ | |
+ $element['preferred_countries'] = [ | |
+ '#type' => 'select', | |
+ '#title' => $this->t('Preferred countries'), | |
+ '#options' => $country_list, | |
+ '#default_value' => $this->getSetting('preferred_countries'), | |
+ '#empty_value' => '', | |
+ '#multiple' => TRUE, | |
+ '#size' => 10, | |
+ ]; | |
+ | |
+ $element['wrapper_type'] = [ | |
+ '#type' => 'radios', | |
+ '#title' => $this->t('Wrapper type'), | |
+ '#options' => $this->wrapperTypeOptions(), | |
+ '#default_value' => $this->getSetting('wrapper_type'), | |
]; | |
+ | |
+ return $element; | |
} | |
/** | |
@@ -122,9 +141,26 @@ class AddressDefaultWidget extends WidgetBase implements ContainerFactoryPluginI | |
*/ | |
public function settingsSummary() { | |
$summary = parent::settingsSummary(); | |
+ | |
+ // Add summary for preferred countries setting. | |
+ $preferred_countries = $this->getSetting('preferred_countries'); | |
+ if (!empty($preferred_countries)) { | |
+ $country_list = $this->countryRepository->getList(); | |
+ foreach ($preferred_countries as &$preferred_country) { | |
+ $preferred_country = $country_list[$preferred_country]; | |
+ } | |
+ $summary[] = $this->formatPlural(count($preferred_countries), | |
+ 'Preferred country: @countries', | |
+ 'Preferred countries: @countries', | |
+ ['@countries' => implode(', ', $preferred_countries)] | |
+ ); | |
+ } | |
+ | |
+ // Add summary for wrapper type setting. | |
$summary[] = $this->t('Wrapper type: @wrapper_type', [ | |
'@wrapper_type' => $this->wrapperTypeOptions()[$this->getSetting('wrapper_type')], | |
]); | |
+ | |
return $summary; | |
} | |
@@ -170,6 +206,7 @@ class AddressDefaultWidget extends WidgetBase implements ContainerFactoryPluginI | |
'#required' => $required, | |
'#available_countries' => $item->getAvailableCountries(), | |
'#field_overrides' => $item->getFieldOverrides(), | |
+ '#preferred_countries' => $this->getSetting('preferred_countries'), | |
]; | |
// Make sure no properties are required on the default value widget. | |
if ($this->isDefaultValueWidget($form_state)) { | |
diff --git a/src/Plugin/Field/FieldWidget/CountryDefaultWidget.php b/src/Plugin/Field/FieldWidget/CountryDefaultWidget.php | |
index 6a2b1e3..ff0152f 100644 | |
--- a/src/Plugin/Field/FieldWidget/CountryDefaultWidget.php | |
+++ b/src/Plugin/Field/FieldWidget/CountryDefaultWidget.php | |
@@ -68,6 +68,65 @@ class CountryDefaultWidget extends WidgetBase implements ContainerFactoryPluginI | |
); | |
} | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public static function defaultSettings() { | |
+ return [ | |
+ 'preferred_countries' => [], | |
+ ] + parent::defaultSettings(); | |
+ } | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public function settingsForm(array $form, FormStateInterface $form_state) { | |
+ $element = []; | |
+ | |
+ $country_list = $this->countryRepository->getList(); | |
+ $available_countries = $this->getFieldSetting('available_countries'); | |
+ if (!empty($available_countries)) { | |
+ $country_list = array_intersect_key($country_list, $available_countries); | |
+ } | |
+ | |
+ $element['preferred_countries'] = [ | |
+ '#type' => 'select', | |
+ '#title' => $this->t('Preferred countries'), | |
+ '#options' => $country_list, | |
+ '#default_value' => $this->getSetting('preferred_countries'), | |
+ '#empty_value' => '', | |
+ '#multiple' => TRUE, | |
+ '#size' => 10, | |
+ ]; | |
+ | |
+ return $element; | |
+ } | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public function settingsSummary() { | |
+ $summary = []; | |
+ | |
+ // Display summary for preferred countries setting. | |
+ $preferred_countries = $this->getSetting('preferred_countries'); | |
+ if (!empty($preferred_countries)) { | |
+ if (!isset($country_list)) { | |
+ $country_list = $this->countryRepository->getList(); | |
+ } | |
+ foreach ($preferred_countries as &$preferred_country) { | |
+ $preferred_country = $country_list[$preferred_country]; | |
+ } | |
+ $summary['preferred_countries'] = $this->formatPlural(count($preferred_countries), | |
+ 'Preferred country: @countries', | |
+ 'Preferred countries: @countries', | |
+ ['@countries' => implode(', ', $preferred_countries)] | |
+ ); | |
+ } | |
+ | |
+ return $summary; | |
+ } | |
+ | |
/** | |
* {@inheritdoc} | |
*/ | |
@@ -76,6 +135,7 @@ class CountryDefaultWidget extends WidgetBase implements ContainerFactoryPluginI | |
'#type' => 'address_country', | |
'#default_value' => $items[$delta]->value, | |
'#available_countries' => $items[$delta]->getAvailableCountries(), | |
+ '#preferred_countries' => $this->getSetting('preferred_countries'), | |
] + $element; | |
return $element; | |
diff --git a/src/Plugin/views/filter/Country.php b/src/Plugin/views/filter/Country.php | |
index 2462f97..13f0f7d 100644 | |
--- a/src/Plugin/views/filter/Country.php | |
+++ b/src/Plugin/views/filter/Country.php | |
@@ -2,6 +2,9 @@ | |
namespace Drupal\address\Plugin\views\filter; | |
+use Drupal\address\PreferredCountriesTrait; | |
+use Drupal\Core\Form\FormStateInterface; | |
+ | |
/** | |
* Filter by country. | |
* | |
@@ -11,6 +14,8 @@ namespace Drupal\address\Plugin\views\filter; | |
*/ | |
class Country extends CountryAwareInOperatorBase { | |
+ use PreferredCountriesTrait; | |
+ | |
/** | |
* {@inheritdoc} | |
*/ | |
@@ -22,4 +27,50 @@ class Country extends CountryAwareInOperatorBase { | |
return $this->valueOptions; | |
} | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public function buildExposedForm(&$form, FormStateInterface $form_state) { | |
+ parent::buildExposedForm($form, $form_state); | |
+ | |
+ // We're implementing preferred_countries and the optgroup support only here | |
+ // in the exposed filter select, so as not to break the admin UI default | |
+ // value form's checkboxes, which don't handle nested arrays properly. | |
+ $preferred_countries = $this->options['preferred_countries']; | |
+ if (!empty($preferred_countries)) { | |
+ $form_key = $this->options['expose']['identifier']; | |
+ $form[$form_key]['#options'] = self::rebuildCountryList($form[$form_key]['#options'], $preferred_countries); | |
+ } | |
+ } | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ protected function defineOptions() { | |
+ $options = parent::defineOptions(); | |
+ $options['preferred_countries'] = ['default' => []]; | |
+ return $options; | |
+ } | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public function buildOptionsForm(&$form, FormStateInterface $form_state) { | |
+ parent::buildOptionsForm($form, $form_state); | |
+ $form['preferred_countries'] = [ | |
+ '#type' => 'select', | |
+ '#title' => $this->t('Preferred countries'), | |
+ '#options' => $this->getAvailableCountries(), | |
+ '#multiple' => TRUE, | |
+ '#default_value' => $this->options['preferred_countries'], | |
+ '#states' => [ | |
+ 'visible' => [ | |
+ ':input[name="options[expose_button][checkbox][checkbox]"]' => [ | |
+ 'checked' => TRUE, | |
+ ], | |
+ ], | |
+ ], | |
+ ]; | |
+ } | |
+ | |
} | |
diff --git a/src/PreferredCountriesTrait.php b/src/PreferredCountriesTrait.php | |
new file mode 100644 | |
index 0000000..058635d | |
--- /dev/null | |
+++ b/src/PreferredCountriesTrait.php | |
@@ -0,0 +1,62 @@ | |
+<?php | |
+ | |
+namespace Drupal\address; | |
+ | |
+use Drupal\Core\StringTranslation\TranslatableMarkup; | |
+ | |
+/** | |
+ * Provides a trait for handling preferred countries. | |
+ */ | |
+trait PreferredCountriesTrait { | |
+ | |
+ /** | |
+ * Rebuilds a given country list to provide for select '#options'. | |
+ * | |
+ * Takes preferred countries and a possible "All" value into account, | |
+ * grouping all non-preferred countries in an "Other" optgroup. | |
+ * | |
+ * @var string[] $country_list | |
+ * Associative array of available countries, keyed by country code, with | |
+ * the values being translated country names, already sorted alphabetically | |
+ * by country name. | |
+ * @var string[] $preferred_candidates | |
+ * Associative array of preferred countries, keyed by country code. The | |
+ * values don't matter. These are only candidates, as they need to appear | |
+ * in the $country_list as well. | |
+ * | |
+ * @return string[] | |
+ * Array of country options, with preferred candidates going first in list. | |
+ */ | |
+ public static function rebuildCountryList($country_list, $preferred_candidates) { | |
+ // See if there really are preferred countries. | |
+ $preferred_countries = array_intersect_key($country_list, $preferred_candidates); | |
+ if (empty($preferred_countries)) { | |
+ return $country_list; | |
+ } | |
+ | |
+ // Start afresh, prepending the 'All' element, if present. | |
+ $country_options = []; | |
+ if (!empty($country_list['All'])) { | |
+ $country_options += [ | |
+ 'All' => $country_list['All'], | |
+ ]; | |
+ unset($country_list['All']); | |
+ } | |
+ | |
+ // Add 'Preferred countries' and purge from the input list. | |
+ $country_options += $preferred_countries; | |
+ foreach (array_keys($preferred_countries) as $country_code) { | |
+ unset($country_list[$country_code]); | |
+ } | |
+ | |
+ // Render all other options, if any, in an 'Other' optgroup. | |
+ if (!empty($country_list)) { | |
+ $other_label = (string) new TranslatableMarkup('Other'); | |
+ $country_options += [ | |
+ $other_label => $country_list, | |
+ ]; | |
+ } | |
+ return $country_options; | |
+ } | |
+ | |
+} | |
diff --git a/tests/modules/address_test/config/install/core.entity_form_display.node.address_test.default.yml b/tests/modules/address_test/config/install/core.entity_form_display.node.address_test.default.yml | |
new file mode 100644 | |
index 0000000..cf5936d | |
--- /dev/null | |
+++ b/tests/modules/address_test/config/install/core.entity_form_display.node.address_test.default.yml | |
@@ -0,0 +1,65 @@ | |
+langcode: en | |
+status: true | |
+dependencies: | |
+ config: | |
+ - field.field.node.address_test.field_address_test | |
+ - node.type.address_test | |
+ module: | |
+ - address | |
+id: node.address_test.default | |
+targetEntityType: node | |
+bundle: address_test | |
+mode: default | |
+content: | |
+ created: | |
+ type: datetime_timestamp | |
+ weight: 2 | |
+ region: content | |
+ settings: { } | |
+ third_party_settings: { } | |
+ field_address_test: | |
+ type: address_default | |
+ weight: 7 | |
+ region: content | |
+ settings: { } | |
+ third_party_settings: { } | |
+ promote: | |
+ type: boolean_checkbox | |
+ weight: 3 | |
+ region: content | |
+ settings: | |
+ display_label: true | |
+ third_party_settings: { } | |
+ status: | |
+ type: boolean_checkbox | |
+ weight: 6 | |
+ region: content | |
+ settings: | |
+ display_label: true | |
+ third_party_settings: { } | |
+ sticky: | |
+ type: boolean_checkbox | |
+ weight: 4 | |
+ region: content | |
+ settings: | |
+ display_label: true | |
+ third_party_settings: { } | |
+ title: | |
+ type: string_textfield | |
+ weight: 0 | |
+ region: content | |
+ settings: | |
+ size: 60 | |
+ placeholder: '' | |
+ third_party_settings: { } | |
+ uid: | |
+ type: entity_reference_autocomplete | |
+ weight: 1 | |
+ region: content | |
+ settings: | |
+ match_operator: CONTAINS | |
+ match_limit: 10 | |
+ size: 60 | |
+ placeholder: '' | |
+ third_party_settings: { } | |
+hidden: { } | |
diff --git a/tests/modules/address_test/config/optional/views.view.address_test_preferred_countries.yml b/tests/modules/address_test/config/optional/views.view.address_test_preferred_countries.yml | |
new file mode 100644 | |
index 0000000..aab8ecc | |
--- /dev/null | |
+++ b/tests/modules/address_test/config/optional/views.view.address_test_preferred_countries.yml | |
@@ -0,0 +1,224 @@ | |
+langcode: en | |
+status: true | |
+dependencies: | |
+ config: | |
+ - node.type.address_test | |
+ module: | |
+ - address | |
+ - node | |
+ - user | |
+id: address_test_preferred_countries | |
+label: 'Address test preferred countries' | |
+module: views | |
+description: '' | |
+tag: '' | |
+base_table: node_field_data | |
+base_field: nid | |
+display: | |
+ default: | |
+ id: default | |
+ display_title: Default | |
+ display_plugin: default | |
+ position: 0 | |
+ display_options: | |
+ title: 'Address test preferred countries' | |
+ fields: | |
+ title: | |
+ id: title | |
+ table: node_field_data | |
+ field: title | |
+ relationship: none | |
+ group_type: group | |
+ admin_label: '' | |
+ entity_type: node | |
+ entity_field: title | |
+ plugin_id: field | |
+ label: '' | |
+ exclude: false | |
+ alter: | |
+ alter_text: false | |
+ make_link: false | |
+ absolute: false | |
+ word_boundary: false | |
+ ellipsis: false | |
+ strip_tags: false | |
+ trim: false | |
+ html: false | |
+ element_type: '' | |
+ element_class: '' | |
+ element_label_type: '' | |
+ element_label_class: '' | |
+ element_label_colon: true | |
+ element_wrapper_type: '' | |
+ element_wrapper_class: '' | |
+ element_default_classes: true | |
+ empty: '' | |
+ hide_empty: false | |
+ empty_zero: false | |
+ hide_alter_empty: true | |
+ click_sort_column: value | |
+ type: string | |
+ settings: | |
+ link_to_entity: true | |
+ group_column: value | |
+ group_columns: { } | |
+ group_rows: true | |
+ delta_limit: 0 | |
+ delta_offset: 0 | |
+ delta_reversed: false | |
+ delta_first_last: false | |
+ multi_type: separator | |
+ separator: ', ' | |
+ field_api_classes: false | |
+ pager: | |
+ type: mini | |
+ options: | |
+ offset: 0 | |
+ items_per_page: 10 | |
+ total_pages: null | |
+ id: 0 | |
+ tags: | |
+ next: ›› | |
+ previous: ‹‹ | |
+ expose: | |
+ items_per_page: false | |
+ items_per_page_label: 'Items per page' | |
+ items_per_page_options: '5, 10, 25, 50' | |
+ items_per_page_options_all: false | |
+ items_per_page_options_all_label: '- All -' | |
+ offset: false | |
+ offset_label: Offset | |
+ exposed_form: | |
+ type: basic | |
+ options: | |
+ submit_button: Apply | |
+ reset_button: false | |
+ reset_button_label: Reset | |
+ exposed_sorts_label: 'Sort by' | |
+ expose_sort_order: true | |
+ sort_asc_label: Asc | |
+ sort_desc_label: Desc | |
+ access: | |
+ type: perm | |
+ options: | |
+ perm: 'access content' | |
+ cache: | |
+ type: tag | |
+ options: { } | |
+ empty: { } | |
+ sorts: { } | |
+ arguments: { } | |
+ filters: | |
+ status: | |
+ id: status | |
+ table: node_field_data | |
+ field: status | |
+ entity_type: node | |
+ entity_field: status | |
+ plugin_id: boolean | |
+ value: '1' | |
+ group: 1 | |
+ expose: | |
+ operator: '' | |
+ operator_limit_selection: false | |
+ operator_list: { } | |
+ type: | |
+ id: type | |
+ table: node_field_data | |
+ field: type | |
+ entity_type: node | |
+ entity_field: type | |
+ plugin_id: bundle | |
+ value: | |
+ address_test: address_test | |
+ expose: | |
+ operator_limit_selection: false | |
+ operator_list: { } | |
+ field_address_test_country_code: | |
+ id: field_address_test_country_code | |
+ table: node__field_address_test | |
+ field: field_address_test_country_code | |
+ relationship: none | |
+ group_type: group | |
+ admin_label: '' | |
+ plugin_id: country | |
+ operator: in | |
+ value: { } | |
+ group: 1 | |
+ exposed: true | |
+ expose: | |
+ operator_id: field_address_test_country_code_op | |
+ label: 'Address (field_address_test:country_code)' | |
+ description: '' | |
+ use_operator: false | |
+ operator: field_address_test_country_code_op | |
+ operator_limit_selection: false | |
+ operator_list: { } | |
+ identifier: field_address_test_country_code | |
+ required: false | |
+ remember: false | |
+ multiple: false | |
+ remember_roles: | |
+ authenticated: authenticated | |
+ anonymous: '0' | |
+ content_editor: '0' | |
+ administrator: '0' | |
+ test_new_role: '0' | |
+ reduce: false | |
+ is_grouped: false | |
+ group_info: | |
+ label: '' | |
+ description: '' | |
+ identifier: '' | |
+ optional: true | |
+ widget: select | |
+ multiple: false | |
+ remember: false | |
+ default_group: All | |
+ default_group_multiple: { } | |
+ group_items: { } | |
+ preferred_countries: { } | |
+ style: | |
+ type: default | |
+ row: | |
+ type: fields | |
+ query: | |
+ type: views_query | |
+ options: | |
+ query_comment: '' | |
+ disable_sql_rewrite: false | |
+ distinct: false | |
+ replica: false | |
+ query_tags: { } | |
+ relationships: { } | |
+ header: { } | |
+ footer: { } | |
+ display_extenders: { } | |
+ cache_metadata: | |
+ max-age: -1 | |
+ contexts: | |
+ - 'languages:language_content' | |
+ - 'languages:language_interface' | |
+ - url | |
+ - url.query_args | |
+ - 'user.node_grants:view' | |
+ - user.permissions | |
+ tags: { } | |
+ page_1: | |
+ id: page_1 | |
+ display_title: Page | |
+ display_plugin: page | |
+ position: 1 | |
+ display_options: | |
+ display_extenders: { } | |
+ path: address-test/views/preferred-countries | |
+ cache_metadata: | |
+ max-age: -1 | |
+ contexts: | |
+ - 'languages:language_content' | |
+ - 'languages:language_interface' | |
+ - url | |
+ - url.query_args | |
+ - 'user.node_grants:view' | |
+ - user.permissions | |
+ tags: { } | |
diff --git a/tests/src/FunctionalJavascript/AddressPreferredCountriesTest.php b/tests/src/FunctionalJavascript/AddressPreferredCountriesTest.php | |
new file mode 100644 | |
index 0000000..4712e56 | |
--- /dev/null | |
+++ b/tests/src/FunctionalJavascript/AddressPreferredCountriesTest.php | |
@@ -0,0 +1,100 @@ | |
+<?php | |
+ | |
+namespace Drupal\Tests\address\FunctionalJavascript; | |
+ | |
+use Drupal\FunctionalJavascriptTests\WebDriverTestBase; | |
+use Drupal\views\Views; | |
+ | |
+/** | |
+ * Tests the preferred countries settings. | |
+ * | |
+ * @group address | |
+ */ | |
+class AddressPreferredCountriesTest extends WebDriverTestBase { | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ protected static $modules = [ | |
+ 'address', | |
+ 'address_test', | |
+ 'node', | |
+ 'views', | |
+ ]; | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ protected $defaultTheme = 'stark'; | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ protected function setUp(): void { | |
+ parent::setUp(); | |
+ | |
+ $this->drupalLogin($this->drupalCreateUser([ | |
+ 'access content', | |
+ 'create address_test content', | |
+ ])); | |
+ } | |
+ | |
+ /** | |
+ * Tests the preferred countries setting for country exposed filter. | |
+ */ | |
+ public function testViewsPreferredCountriesExposedSetting() { | |
+ // Set up views filter configuration. | |
+ $view = Views::getView('address_test_preferred_countries'); | |
+ $filters = $view->getDisplay()->getOption('filters'); | |
+ $filters['field_address_test_country_code']['preferred_countries'] = [ | |
+ 'CA' => 'CA', | |
+ 'GB' => 'GB', | |
+ 'US' => 'US', | |
+ ]; | |
+ $view->getDisplay()->overrideOption('filters', $filters); | |
+ $view->save(); | |
+ | |
+ // Go to views page with exposed filters and confirm filter exists. | |
+ $this->drupalGet('address-test/views/preferred-countries'); | |
+ $this->assertSession()->elementExists('xpath', '//select[@name="field_address_test_country_code"]'); | |
+ | |
+ // Make sure configured preferred countries are displayed first. | |
+ $elements = $this->xpath('//select[@name="field_address_test_country_code"]/option'); | |
+ $preferred_options = []; | |
+ foreach ($elements as $element) { | |
+ if ($option = $element->getValue()) { | |
+ $preferred_options[] = $option; | |
+ } | |
+ } | |
+ $this->assertEquals(['All', 'CA', 'GB', 'US'], $preferred_options, 'Preferred countries are not displayed first in the exposed filter select.'); | |
+ } | |
+ | |
+ /** | |
+ * Tests the preferred countries setting for widget. | |
+ */ | |
+ public function testWidgetPreferredCountriesSetting() { | |
+ // Adjust widget settings to have preferred countries. | |
+ $display = \Drupal::configFactory() | |
+ ->getEditable('core.entity_form_display.node.address_test.default'); | |
+ $display->set('content.field_address_test.settings.preferred_countries', [ | |
+ 'CA', | |
+ 'GB', | |
+ 'US', | |
+ ])->save(); | |
+ | |
+ // Go to node add page and confirm field exists. | |
+ $this->drupalGet('node/add/address_test'); | |
+ $this->assertSession()->elementExists('xpath', '//select[@name="field_address_test[0][address][country_code]"]'); | |
+ | |
+ // Make sure configured preferred countries are displayed first. | |
+ $elements = $this->xpath('//select[@name="field_address_test[0][address][country_code]"]/option'); | |
+ $preferred_options = []; | |
+ foreach ($elements as $element) { | |
+ if ($option = $element->getValue()) { | |
+ $preferred_options[] = $option; | |
+ } | |
+ } | |
+ $this->assertEquals(['CA', 'GB', 'US'], $preferred_options, 'Preferred countries are not displayed first in the exposed filter select.'); | |
+ } | |
+ | |
+} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment