kadegray / statamic-country-and-region-fieldtypes

Statamic Country And Region Fieldtypes is an addon that is everything you need to store and display Country and Region on your site.
2 stars 0 forks source link

Region fieldtype does not detect country field within replicator #9

Open Boefjim opened 3 weeks ago

Boefjim commented 3 weeks ago

When using both the country & region fieldtypes within a replicator, the country field at the root of the blueprint is still used to determine the possible regions.

Would it be possible to use the country field within the replicator instead?

kadegray commented 3 weeks ago

Hi @Boefjim,

Thank you for creating this issue. It's an interesting one.

Please confirm I have this right:

  1. You're using a Country Fieldtype and a Region Fieldtype within a replicator?
  2. All Region Fieldtypes listed look to the first Country Fieldtype to filter their regions by?

If the above is true, configuring 'Field' config on the Region Fieldtype will produce this issue within a replicator. I think in this situation you would have to use the Region in Country Fieldtype. Would this work for your use case?

Otherwise I'll be able to take a look at this next week.

kadegray commented 3 weeks ago

I had a quick look at Region in Country Fieldtype to confirm. I believe it will work as expected in a replicator because it is not using the handle of the Country Fieldtype. It is its own isolated Fieldtype.

Please let me know whether it is suitable.

Boefjim commented 3 weeks ago
  1. Yes, I have both fields within the same set in the replicator, one Country Fieldtype field & one Region Fieldtype field
  2. With multiple entries in the replicator, each Region Fieldtype field has their options based on the configured country field outside of the replicator (by default the field with the country handle), since the handle should be unique by the rules of statamic, there is not really a "first".

Using the Region in Country Fieldtype sadly is not a fitting alternative for my use case, because it should be possible to select a country without any region, but that fieldtype does not return any value when no region is selected.

Technically it would be possible to have both the Country Fieldtype field and a Region in Country fieldtype field and then select the same country again in the Region in Country Fieldtype field, but that is an extra step for the content editor that I hope would not be necessary.

kadegray commented 3 weeks ago

I understand.

As I understand it the Replicator Fieldtype on save saves its entries essentially as an array. Which works because you define that structure with the blueprint.

On the frontend (the control panel), in the case of Region Fieldtype, it is referencing the Country Fieldtype by handle. It has a default handle, they should be unique, and handles are in the control of the user/blueprint. I don't believe it would be possible to define the relationship between each Country Fieldtype and Region Fieldtype within a Replicator.

However, I can probably add a feature to the Region in Country Fieldtype, or an entirely new Fieldtype, that makes selecting region optional.

I can probably look at this next week.

Boefjim commented 2 weeks ago

Hey @kadegray were you able to look at this yet?

Would love to have the new field (feature) available to use!

kadegray commented 1 week ago

@Boefjim I've started looking at this now.

kadegray commented 1 week ago

@Boefjim Just an update.

So the Region in Country Fieldtype within a replicator, on initial save, if you only select country and save, will have only the country (as ISO 3166-1; "US") saved to the entry.

However, there are two bugs:

  1. Once a region is selected and you clear it, the value of the field remains the previously selected region. If you then save it saves said previous region.
  2. After saving the entry, if the page is freshly loaded (that is 'Continue Editing' on the 'Edit & Save' button is not enabled; meaning you click on the entry again after saving). The country field cannot be cleared. It can effectively be reset by selecting another country.

So basically what is currently happening is when a region is selected in the Region in Country Fieldtype is that it gets stuck either saving the previously selected region (selected before clearing) or a currently selected region. But you can't go back to only country unless you select another country.

I'm going to focus on fixing these bugs, at the moment I have a few hours each day.

kadegray commented 2 days ago

@Boefjim I've released changes in 1.2.6, please run composer update kadegray/statamic-country-and-region-fieldtypes.

You will see now on the in the Region in Country fieldtype that there is a config called Region is required. By default it is enabled, however if you disable it it will permit only country to be saved.