Problem/Motivation

When fetching an Address field over JSON:API, you get all of the field properties. Even if they are not used.

Given the following base field definition:

    $fields['address'] = BaseFieldDefinition::create('address')
      ->setLabel('Address')
      ->setRequired(TRUE)
      ->setSetting('field_overrides', [
        AddressField::GIVEN_NAME => ['override' => FieldOverride::HIDDEN],
        AddressField::FAMILY_NAME => ['override' => FieldOverride::HIDDEN],
        AddressField::ORGANIZATION => ['override' => FieldOverride::HIDDEN],
        AddressField::ADDRESS_LINE1 => ['override' => FieldOverride::OPTIONAL],
        AddressField::ADDRESS_LINE2 => ['override' => FieldOverride::OPTIONAL],
      ]);

We have the following output

      "attributes": {
        "name": "Nuclear Coffee ",
        "email": "sales@nuclearcoffeewi.com",
        "phone": "(262) 290-5137",
        "website": "https:\/\/nuclearcoffeewi.com\/",
        "address": {
          "langcode": null,
          "country_code": "US",
          "administrative_area": "WI",
          "locality": "Milwaukee",
          "dependent_locality": null,
          "postal_code": "53228",
          "sorting_code": null,
          "address_line1": null,
          "address_line2": null,
          "organization": null,
          "given_name": null,
          "additional_name": null,
          "family_name": null
        }
      }
    }

Steps to reproduce

Set up an address field with field overrides and fetch the entity over JSON:API

Proposed resolution

\Drupal\jsonapi\Normalizer\FieldItemNormalizer calls \Drupal\Core\TypedData\ComplexDataInterface::getProperties. Which for fields is implemented in \Drupal\Core\TypedData\Plugin\DataType\Map::getProperties.

The AddressItem field class could override this method to exclude properties that over overridden as hidden.

Issue fork address-3195984

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mglaman created an issue. See original summary.

mglaman’s picture

With this:

   "attributes": {
    "name": "Nuclear Coffee ",
    "email": "sales@nuclearcoffeewi.com",
    "phone": "(262) 290-5137",
    "website": "https://nuclearcoffeewi.com/",
    "address": {
     "langcode": null,
     "country_code": "US",
     "administrative_area": "WI",
     "locality": "Milwaukee",
     "dependent_locality": null,
     "postal_code": "53228",
     "sorting_code": null,
     "address_line1": null,
     "address_line2": null
    }
   }
  }
mglaman’s picture

Status: Active » Needs review

bojanz made their first commit to this issue’s fork.

bojanz’s picture

Status: Needs review » Fixed

Merged, thanks!

Status: Fixed » Closed (fixed)

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