Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dictionary fieldtype] Improve Countries dictionary - create a solution for similar use cases #1257

Open
ebeauchamps opened this issue Nov 5, 2024 · 0 comments

Comments

@ebeauchamps
Copy link

ebeauchamps commented Nov 5, 2024

Why is this FR worth it?
First: Countries dictionary appears to be able to filter countries by subregions, but this has not be fully implemented yet

Second: there are many similar cases that could be handled by creating a custom dictionary based on this countries dictionary

  • countries and states (or regions, or landers...)
  • states and counties (or smaller administrative regions within a big region)
  • counties and cities
  • cities and districts
    and so on

Thinking for example of a city picker that allows for choosing a district where a specific service could be proposed.

1_ Create a relationship between regions and their subregions

One subregion is related to one region only: I am thinking of something like that (?)
'africa' => ['africa', 'subregion' => $this->subregions['central_africa', 'eastern_africa', 'northern_africa', 'southern_africa', 'south_eastern_africa']],
'europe' => ['europe', 'subregion' => $this->subregions ['eastern_europe', 'northern_europe', 'southern_europe', 'western_europe']]
And so on.

2_Add the subregions in the field items:
2.1
protected function fieldItems()
{
return [
'region' => [
'display' => __('Region'),
'instructions' => __('statamic::messages.dictionaries_countries_region_instructions'),
'type' => 'select',
'options' => $this->regions,
],
'subregion' => [
'display' => 'Subregion',
'instructions' => 'Filter regions by subregions.',
'type' => 'select',
'options' => $this->subregions,
],
];
}

2.2
CRITICAL: Restrict available values of subregions only to the ones related to the regions selected.
Currently, you can add 2.1 above in the Countries dictionary, it's working but ALL subregions are available whatever the region you select first.

3_Unselecting region or subregion
Provide a solution to easily unselect either a subregion or a region. Those fields are optional but somehow you cannot unselect your choice and must choose another Dictionary type to erase everything. Cumbersome.

4_Improve function getFilteredItems(): Collection
4.1 Add a test for the subregion:
->when($this->config['subregion'] ?? false, fn ($collection, $subregion) => $collection->where('subregion', $this->subregions[$subregion]));

4.2 But when a subregion isn't selected, the function should check for the region. What I wrote below isn't working but this is just a hint a what I would like to achieve:
->when(($this->config['region'] ?? false) and ($this->config['subregion'] === false ), fn ($collection, $region) => $collection->where('region', $this->regions[$region]));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant