Country field is set to none for the existing nodes even though the address field is set to default country

Created on 6 April 2023, about 1 year ago
Updated 12 January 2024, 6 months ago

Country is set to none in the existing nodes even when the country is set to default in the field settings

1. Add address field to an existing content type(which has nodes already)
2. Set the country to US in the country drop down in the field settings.
3. Works as expected while creating new nodes.
4. But when you edit an existing node to add address, country is set to none instead of setting to the field default setting(which is U.s in our case)

The field setting should be applied to the existing nodes as well instead of changing it to none

Remaining tasks

User interface changes

API changes

Data model changes

πŸ’¬ Support request
Status

Fixed

Version

1.11

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @irsar
  • πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)

    We ran into this as well, and it was a case of case-sensitivity in the data. We had "us" in the database instead of "US".

    We fixed it in an update hook:

      $database = \Drupal::database();
      $tables = ['node__field_address', 'node_revision__field_address'];
      foreach ($tables as $table) {
        $query = $database->select($table, 'nfa')
          ->where('binary field_address_country_code = :code',
            ['code' => "us"])
          ->fields('nfa', ['entity_id'])
          ->execute();
        $result = $query->fetchCol();
        foreach ($result as $id) {
          $query = $database->update($table);
          $query->fields(['field_address_country_code' => 'US']);
          $query->condition('entity_id', $id);
          $query->execute();
        }
      }
    

    Find your address field and run a query like this one to check:

    select entity_id, field_address_country_code FROM node__field_address where field_address_country_code= BINARY 'us';
    

    At the module level, it might be nice to UPPERCASE country codes in all lookups.

  • Status changed to Fixed 6 months ago
  • πŸ‡·πŸ‡ΈSerbia bojanz

    I would prefer to let the sites be responsible for the validity of their data, because silently returning lowercase country codes could break other modules, REST clients, etc.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024