Skip to content

Dependent location fields

Version v1

Initialising viewer…

You can apply this model as a recipe (Drupal 10.3 or later) to your own Drupal site:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
## Import recipe
composer require drupal-eca-recipe/eca_lib_0040

# Apply recipe with Drush (requires version 13 or later):
drush recipe ../recipes/eca_lib_0040

# Apply recipe without Drush:
cd web && php core/scripts/drupal recipe ../recipes/eca_lib_0040

# Rebuilding caches is optional, sometimes required:
drush cr

Purpose

This model provides dependent Country, State or province, and City select fields on the Location content form. Choosing a country refreshes the available states or provinces through Ajax. Choosing a state or province refreshes the available cities.

How it works

The form build event adds Ajax handlers to the Country and State or province fields. Each form rebuild first clears the dependent options. Conditions compare the submitted form values and then populate only the matching downstream options.

Test data

The compact demonstration data contains Canada, Germany, and the United States. Each country provides three states or provinces, and each state or province provides two cities. Country and subdivision keys use ISO 3166 codes where applicable.

Dependencies

  • config
    • field.field.node.location.field_city
    • field.field.node.location.field_country
    • field.field.node.location.field_state
    • field.storage.node.field_city
    • field.storage.node.field_country
    • field.storage.node.field_state
    • node.type.location
  • module
    • eca_base
    • eca_form
    • menu_ui
    • modeler_api
    • node
    • options

Used plugins

Events

Conditions

Actions

Changelog

Initial version